GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — SQL - DATETIME2FROMPARTS() Function
Selected Reading
SQL - DATETIME2FROMPARTS() FunctionThe SQL DATETIME2FROMPARTS() function is used to construct a datetime2 value from an individual date and time segments. This function accepts eight parameters such as year, month, day, hour, minute, second, and precision, and returns a datetime2 value that represents the specified time.
This function is mostly used when there are separate values for the year, month, day, hour, minute, second, and fractional seconds of a datetime2 value and want to combine them into a single datetime2 value. SyntaxFollowing is the syntax of the SQL DATETIME2FROMPARTS() function − DATETIME2FROMPARTS(year, month, day, hour, minute, seconds, fractions, precision) ParametersThis function accepts eight parameters. The same is described below −
ExampleIn the following example, we are trying to construct a datetime2 value without the fractions of a second using the following query − SQL> SELECT DATETIME2FROMPARTS(2023, 02, 20, 06, 10, 45, 0, 0) AS RESULT; OutputOn executing the above query, the output is displayed as follows − +---------------------+ | RESULT | +---------------------+ | 2023-02-20 06:10:45 | +---------------------+ ExampleLet us try to use the fractions and precision parameters of the SQL DATETIME2FROMPARTS() function −
SQL> SELECT DATETIME2FROMPARTS(2023, 02, 20, 06, 10, 45, 5, 1) AS RESULT_1; SQL> SELECT DATETIME2FROMPARTS(2023, 02, 20, 06, 10, 45, 5, 2) AS RESULT_2; SQL> SELECT DATETIME2FROMPARTS(2023, 02, 20, 06, 10, 45, 5, 3) AS RESULT_3; OutputIf we execute the program, the result is produced as follows − +-----------------------+ | RESULT | +-----------------------+ | 2023-02-20 06:10:45.5 | +-----------------------+ +------------------------+ | RESULT | +------------------------+ | 2023-02-20 06:10:45.05 | +------------------------+ +-------------------------+ | RESULT | +-------------------------+ | 2023-02-20 06:10:45.005 | +-------------------------+ ExampleHere, we are providing invalid values to any of the parameters of the function, it results in an error. SQL> SELECT DATETIME2FROMPARTS(2023, 16, 45, 29, 76, 88, 5, 1) AS RESULT; OutputWhen we execute the above query, the output is obtained as follows − Cannot construct data type datetime2, some of the arguments have values which are not valid. ExampleIf we provide any of the parameters of the function as null, the function will result as null. SQL> SELECT DATETIME2FROMPARTS(2023, null, null, null, 34, 30, 5, 1) AS RESULT; OutputWhen we execute the above query, the output is obtained as follows − +--------+ | RESULT | +--------+ | NULL | +--------+ ExampleWe need to provide all five parameters of the function. If we do not provide it, the function will result in an error. SQL> SELECT DATETIME2FROMPARTS(2023, 02, 20, 06, 10) AS RESULT; OutputIf we execute the above query, the result is produced as follows − The datetime2fromparts function requires 8 argument(s). ExampleAssume we have created a table with the name STUDENTS in the SQL database using the CREATE statement as shown in the query below − SQL> CREATE TABLE STUDENTS(ID INT NOT NULL, NAME VARCHAR (200) NOT NULL, YEARS VARCHAR (200) NOT NULL, MONTHS VARCHAR (200) NOT NULL, DAYS VARCHAR (200) NOT NULL, HOURS VARCHAR (200) NOT NULL, MINUTES VARCHAR (200) NOT NULL, SECONDS VARCHAR (200) NOT NULL); Now, let us insert some records in the STUDENTS table using INSERT statements as shown in the query below − SQL> INSERT INTO STUDENTS(ID, NAME, YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS) VALUES(1, 'Dhruv', '2000', '02', '01', '14', '30', '00'); INSERT INTO STUDENTS(ID, NAME, YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS) VALUES(2, 'Arjun', '2001', '05', '20', '02', '34', '45'); INSERT INTO STUDENTS(ID, NAME, YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS) VALUES(3, 'Dev', '2000', '05', '25', '05', '56', '45'); INSERT INTO STUDENTS(ID, NAME, YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS) VALUES(4, 'Riya', '2003', '01', '10', '17', '21', '23'); INSERT INTO STUDENTS(ID, NAME, YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS) VALUES(5, 'Aarohi','1999', '12', '20', '23', '36', '54'); INSERT INTO STUDENTS(ID, NAME, YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS) VALUES(6, 'Lisa', '2004', '07', '17', '10', '46', '13'); INSERT INTO STUDENTS(ID, NAME, YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS) VALUES(7, 'Roy', '2003', '09', '19', '04', '30', '00'); We can verify whether the table is created or not using the following query − SQL> SELECT * FROM STUDENTS The table STUDENTS is successfully created in the SQL database. +----+-------+-------+--------+------+-------+---------+---------+ | ID | NAME | YEARS | MONTHS | DAYS | HOURS | MINUTES | SECONDS | +----+-------+-------+--------+------+-------+---------+---------+ | 1 | Dhruv | 2000 | 02 | 01 | 14 | 30 | 00 | | 2 | Arjun | 2001 | 05 | 20 | 02 | 34 | 45 | | 3 | Dev | 2000 | 05 | 25 | 05 | 56 | 45 | | 4 | Riya | 2003 | 01 | 10 | 17 | 21 | 23 | | 5 | Aarohi| 1999 | 12 | 20 | 23 | 36 | 54 | | 6 | Lisa | 2004 | 07 | 17 | 10 | 46 | 13 | | 7 | Roy | 2003 | 09 | 19 | 04 | 30 | 00 | +----+-------+-------+--------+------+-------+---------+---------+ Here, we are joining all the datetime2 values of the students to retrieve the students birth date and time using the following query − SQL> SELECT NAME, DATETIME2FROMPARTS(YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS, 0, 2) AS STUDENTS_BIRTH_DATE_AND_TIME FROM STUDENTS; OutputWhen we execute the above query, the output is obtained as follows − +--------+------------------------------+ | NAME | STUDENTS_BIRTH_DATE_AND_TIME | +--------+------------------------------+ | Dhruv | 2000-02-01 14:30:00.00 | | Arjun | 2001-05-20 02:34:45.00 | | Dev | 2000-05-25 05:56:45.00 | | Riya | 2003-01-10 17:21:23.00 | | Aarohi | 1999-12-20 23:36:54.00 | | Lisa | 2004-07-17 10:46:13.00 | | Roy | 2003-09-19 04:30:00.00 | +--------+------------------------------+ sql-date-functions.htm
Advertisements
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |