SQL Server Tutorial SQL Server Advanced SQL Server Database SQL Server References

SQL Server DATEFROMPARTS() Function



The SQL Server (Transact-SQL) DATEFROMPARTS() function returns a date value from the specified parts (year, month, and day values).

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

Syntax

DATEFROMPARTS(year, month, day)

Parameters

year Required. Specify the year of the date value.
month Required. Specify the month of the date value, from 1 to 12.
day Required. Specify the day of the date value.

Return Value

Returns the date value from the specified parts.

Example 1:

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

SELECT DATEFROMPARTS(1999, 10, 25);
Result: '1999-10-25'

SELECT DATEFROMPARTS(2000, 2, 29);
Result: '2000-02-29'

Example 2:

Consider a database table called DateTable with the following records:

IDYearMonthDay
11999323
2200368
320101128
42004814
52012118

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

SELECT *, DATEFROMPARTS(Year, Month, Day) AS DATEFROMPARTS_Value 
FROM DateTable;

This will produce the result as shown below:

IDYearMonthDayDATEFROMPARTS_Value
119993231999-03-23
22003682003-06-08
3201011282010-11-28
420048142004-08-14
520121182012-01-18

❮ SQL Server Functions