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

T-SQL DATETIMEFROMPARTS() Function



The T-SQL (Transact-SQL) DATETIMEFROMPARTS() function returns a datetime value from the specified date and time arguments.

The DATETIMEFROMPARTS() function returns NULL if any of the argument has a null value. For invalid arguments, an error will be raised.

Syntax

DATETIMEFROMPARTS(year, month, day, hour, minute, seconds, milliseconds)

Parameters

year Required. Specify the year of the datetime value.
month Required. Specify the month of the datetime value.
day Required. Specify the day of the datetime value.
hour Required. Specify the hours of the datetime value.
minute Required. Specify the minutes of the datetime value.
seconds Required. Specify the seconds of the datetime value.
milliseconds Required. Specify the milliseconds of the datetime value.

Return Value

Returns the datetime value from the specified parts.

Example 1:

The example below shows the usage of DATETIMEFROMPARTS() function.

SELECT DATETIMEFROMPARTS(1999, 10, 25, 22, 45, 58, 0);
Result: '1999-10-25 22:45:58.000'

SELECT DATETIMEFROMPARTS(1999, 10, 25, 22, 45, 58, 500);
Result: '1999-10-25 22:45:58.500'

Example 2:

Consider a database table called DateTimeTable with the following records:

IDYearMonthDayHoursMinutesSeconds
11999323224555
220036853421
320101128142310
420048149819
5201211881118

The following statement can be used to get the datetime value using the records of various columns of this table.

SELECT *, DATETIMEFROMPARTS(Year, Month, Day, Hours, Minutes, Seconds, 0) 
AS DATETIMEFROMPARTS_Value FROM DateTimeTable;

This will produce the result as shown below:

IDYearMonthDayHoursMinutesSecondsDATETIMEFROMPARTS_Value
119993232245551999-03-23 22:45:55.000
2200368534212003-06-08 05:34:21.000
3201011281423102010-11-28 14:23:10.000
4200481498192004-08-14 09:08:19.000
52012118811182012-01-18 08:11:18.000

❮ T-SQL Functions