SQL Server TIMEFROMPARTS() Function
The SQL Server (Transact-SQL) TIMEFROMPARTS() function returns a time value from the specified time and with the specified precision.
The TIMEFROMPARTS() function returns NULL if any of the argument has a null value. However, if the precision argument is null, then an error is raised. For invalid arguments, an error will be raised.
The fractions argument depends on the precision argument. For example, if precision is 7, then each fraction represents 100 nanoseconds; if precision is 3, then each fraction represents a millisecond. If the value of precision is zero, then the value of fractions must also be zero; otherwise, an error is raised.
Syntax
TIMEFROMPARTS(hour, minute, seconds, fractions, precision)
Parameters
hour |
Required. Specify the hours of the time value. |
minute |
Required. Specify the minutes of the time value. |
seconds |
Required. Specify the seconds of the time value. |
fractions |
Required. Specify the fraction of the time value. |
precision |
Required. Specify the precision of the time value to be returned. |
Return Value
Returns the time value from the specified parts.
Example 1:
The example below shows the usage of TIMEFROMPARTS() function.
SELECT TIMEFROMPARTS(22, 45, 58, 0, 0); Result: '22:45:58.0000000' SELECT TIMEFROMPARTS(22, 45, 58, 4, 1); Result: '22:45:58.4' SELECT TIMEFROMPARTS(22, 45, 58, 40, 2); Result: '22:45:58.40' SELECT TIMEFROMPARTS(22, 45, 58, 400, 3); Result: '22:45:58.400'
Example 2:
Consider a database table called TimeTable with the following records:
ID | Hours | Minutes | Seconds |
---|---|---|---|
1 | 22 | 45 | 55 |
2 | 5 | 34 | 21 |
3 | 14 | 23 | 10 |
4 | 9 | 8 | 19 |
5 | 8 | 11 | 18 |
The following statement can be used to get the time value using the records of various columns of this table.
SELECT *, TIMEFROMPARTS(Hours, Minutes, Seconds, 0, 3) AS TIMEFROMPARTS_Value FROM TimeTable;
This will produce the result as shown below:
ID | Hours | Minutes | Seconds | TIMEFROMPARTS_Value |
---|---|---|---|---|
1 | 22 | 45 | 55 | 22:45:55.000 |
2 | 5 | 34 | 21 | 05:34:21.000 |
3 | 14 | 23 | 10 | 14:23:10.000 |
4 | 9 | 8 | 19 | 09:08:19.000 |
5 | 8 | 11 | 18 | 08:11:18.000 |
❮ SQL Server Functions