SQL - DATETIME2FROMPARTS() Function



The 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.

  • If the values passed as parameters are invalid, this function will result in an error.

  • If any of the parameters are null, it will return the result as null.

  • It returns an error if any of the parameters are missing.

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.

Syntax

Following is the syntax of the SQL DATETIME2FROMPARTS() function −

DATETIME2FROMPARTS(year, month, day, hour, minute, seconds, fractions, precision)

Parameters

This function accepts eight parameters. The same is described below −

  • year − This specifies the year segment of the datetime2 value, expressed as an integer.

  • month − This specifies the month segment of the datetime2 value, expressed as an integer from 1 to 12.

  • day − This specifies the day segment of the datetime2 value, expressed as an integer from 1 to 31.

  • hour − This specifies the hour segment of the datetime2 value, expressed as an integer from 0 to 23.

  • minute − This specifies the minute segment of the datetime2 value, expressed as an integer from 0 to 59.

  • seconds − This specifies the second segment of the datetime2 value, expressed as an integer from 0 to 59.

  • fractions − This specifies the fraction of the datetime2 value, expressed as an integer from 0 to 9999999.

  • precision − This specifies the precision of the datetime2 value to be returned.

Example

In 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;

Output

On executing the above query, the output is displayed as follows −

+---------------------+
| RESULT              |
+---------------------+
| 2023-02-20 06:10:45 |
+---------------------+

Example

Let us try to use the fractions and precision parameters of the SQL DATETIME2FROMPARTS() function −

  • When the value of the fraction is 5 and the precision value is 1, then the value of fractions represents 5/10 of a second.

  • When the value of the fraction is 5 and the precision value is 2, then the value of fractions represents 5/100 of a second.

  • When the value of the fraction is 5 and the precision value is 3, then the value of fractions represents 5/1000 of a second.

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;

Output

If 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 |
+-------------------------+

Example

Here, 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;

Output

When 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.

Example

If 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;  

Output

When we execute the above query, the output is obtained as follows −

+--------+
| RESULT |
+--------+
| NULL   |
+--------+

Example

We 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;  

Output

If we execute the above query, the result is produced as follows −

The datetime2fromparts function requires 8 argument(s).

Example

Assume 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;

Output

When 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