SQL - TIMEFROMPARTS() Function



The SQL TIMEFROMPARTS() function is used to construct a time value from individual segments.

This function accepts five parameters such as hour, minute, seconds, fractions, and precision, and returns a time 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.

Syntax

Following is the syntax of the TIMEFROMPARTS() function in SQL −

TIMEFROMPARTS(hour, minute, seconds, fractions, precision)

Parameters

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

  • hour − This specifies the hour segment of the time value.

  • minute − This specifies the minute segment of the time value.

  • seconds − This specifies the second segment of the time value.

  • fractions − This specifies the fraction of the time value.

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

Example

Let us try to construct a time value without the fractions of a second using the following query −

SQL> SELECT TIMEFROMPARTS(16, 45, 20, 0, 0) AS RESULT;  

Output

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

+----------+
| RESULT   |
+----------+
| 16:45:20 |
+----------+

Example

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

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

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

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

SQL> SELECT TIMEFROMPARTS(16, 45, 20, 5, 1) AS RESULT_1;  
SELECT TIMEFROMPARTS(16, 45, 20, 5, 2) AS RESULT_2;  
SELECT TIMEFROMPARTS(16, 45, 20, 5, 3) AS RESULT_3;

Error

If we execute the program, the result is produced as follows −

+------------+
| RESULT_1   |
+------------+
| 16:45:20.5 |
+------------+

+-------------+
| RESULT_2    |
+-------------+
| 16:45:20.05 |
+-------------+

+--------------+
| RESULT_3     |
+--------------+
| 16:45:20.005 |
+--------------+

Example

If we provide invalid values to any of the parameters of the function, it results an error.

SQL> SELECT TIMEFROMPARTS(16, 4567876, 20, 843567886, 3) AS RESULT;  

Error

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

Cannot construct data type time, 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 TIMEFROMPARTS(16, null, 20, null, 3) 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 TIMEFROMPARTS(16, 30, 20, 45) AS RESULT;

Output

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

The timefromparts function requires 5 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, 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, HOURS, MINUTES, SECONDS) VALUES(1, 'Dhruv', '02', '25', '45');
INSERT INTO STUDENTS(ID, NAME, HOURS, MINUTES, SECONDS) VALUES(2, 'Arjun', '05', '45', '20');
INSERT INTO STUDENTS(ID, NAME, HOURS, MINUTES, SECONDS) VALUES(3, 'Dev', '06','30', '30');
INSERT INTO STUDENTS(ID, NAME, HOURS, MINUTES, SECONDS) VALUES(4, 'Riya', '10', '15', '50');
INSERT INTO STUDENTS(ID, NAME, HOURS, MINUTES, SECONDS) VALUES(5, 'Aarohi', '12', '00', '00');
INSERT INTO STUDENTS(ID, NAME, HOURS, MINUTES, SECONDS) VALUES(6, 'Lisa', '16', '45', '25');
INSERT INTO STUDENTS(ID, NAME, HOURS, MINUTES, SECONDS) VALUES(7, 'Roy', '18', '10', '10');

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   | HOURS | MINUTES | SECONDS |
+-----+--------+-------+---------+---------+
| 1   | Dhruv  | 02    | 25      | 45      |
| 2   | Arjun  | 05    | 45      | 20      |  
| 3   | Dev    | 06    | 30      | 30      |
| 4   | Riya   | 10    | 15      | 50      |
| 5   | Aarohi | 12    | 00      | 00      |
| 6   | Lisa   | 16    | 45      | 25      |
| 7   | Roy    | 18    | 10      | 10      |
+-----+--------+-------+---------+---------+

Here, we are trying to join all the time values of the students using the following query −

SQL> SELECT *, TIMEFROMPARTS(HOURS, MINUTES, SECONDS, 0, 5) AS TIMEFROMPARTS_VALUE FROM STUDENTS;

Output

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

+-----+--------+--------+---------+---------+---------------------+
| ID  | NAME   | HOURS  | MINUTES | SECONDS | TIMEFROMPARTS_VALUE |
+-----+--------+--------+---------+---------+---------------------+
| 1   | Dhruv  | 02     | 25      | 45      | 02:25:45.00000      |
| 2   | Arjun  | 05     | 45      | 20      | 05:45:20.00000      |
| 3   | Dev    | 06     | 30      | 30      | 06:30:30.00000      |
| 4   | Riya   | 10     | 15      | 50      | 10:15:50.00000      |
| 5   | Aarohi | 12     | 00      | 00      | 12:00:00.00000      |
| 6   | Lisa   | 16     | 45      | 25      | 16:45:25.00000      |
| 7   | Roy    | 18     | 10      | 10      | 18:10:10.00000      |
+-----+--------+--------+---------+---------+---------------------+
sql-date-functions.htm
Advertisements