T-SQL Tutorial T-SQL Advanced Database Management T-SQL References

T-SQL TIMEFROMPARTS() Function



The T-SQL (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:

IDHoursMinutesSeconds
1224555
253421
3142310
49819
581118

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:

IDHoursMinutesSecondsTIMEFROMPARTS_Value
122455522:45:55.000
25342105:34:21.000
314231014:23:10.000
4981909:08:19.000
58111808:11:18.000

❮ T-SQL Functions