SQL Server DATEFROMPARTS() Function
The DATEFROMPARTS()
function in SQL Server creates a date value from its individual components: year, month, and day. This is very useful for building dates programmatically, especially when you are working with date data that's stored or calculated as separate year, month, and day values.
DATEFROMPARTS(): Definition and Usage
DATEFROMPARTS()
offers a convenient way to construct a date value, rather than having to manually create a date string. It takes the year, month, and day as input and returns a date value. This makes creating and manipulating dates in your SQL code much cleaner.
Syntax
Syntax
DATEFROMPARTS(year, month, day)
Parameter Values
Parameter | Description |
---|---|
year |
The year (a four-digit integer). This is required. |
month |
The month (an integer from 1 to 12, where 1 is January). This is required. |
day |
The day (an integer from 1 to 31, depending on the month). This is required. |
Example
Creating a Date from its Parts
This example shows how to create the date October 31st, 2018.
Syntax
SELECT DATEFROMPARTS(2018, 10, 31) AS DateFromParts;
Output
2018-10-31