T-SQL DATEFROMPARTS() Function
The T-SQL (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:
ID | Year | Month | Day |
---|---|---|---|
1 | 1999 | 3 | 23 |
2 | 2003 | 6 | 8 |
3 | 2010 | 11 | 28 |
4 | 2004 | 8 | 14 |
5 | 2012 | 1 | 18 |
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:
ID | Year | Month | Day | DATEFROMPARTS_Value |
---|---|---|---|---|
1 | 1999 | 3 | 23 | 1999-03-23 |
2 | 2003 | 6 | 8 | 2003-06-08 |
3 | 2010 | 11 | 28 | 2010-11-28 |
4 | 2004 | 8 | 14 | 2004-08-14 |
5 | 2012 | 1 | 18 | 2012-01-18 |
❮ T-SQL Functions