SQL Server Datefromparts()
It may seem like a simple function, but it can come in handy when you need to create a set of dates from various inputs in a quick and effective method.”
Function Syntax and Parameters
The following illustrates the syntax of the DATEFROMPARTS() function.
The function accepts three main arguments:
- year – this is an integer type denoting the year.
- month – defines the month part of the date as an integer value. Ranges from 1 to 12.
- day – specifies the day part of the date value. Must range from 1 to 31
The function then returns a DATE type from the provided inputs. If either of the provided input is missing or NULL, the function will return a NULL value.
The following example shows a simple usage of the DATEFROMPARTS() function in SQL Server.
The example below uses the DATEFROMPARTS() with NULL input values.
In the example below, we use the DATEFROMPARTS() function with an out-of-range input:
In this case, the function returns an error as shown:
In this short post, we discussed using the DATEFROMPARTS() function in SQL Server to construct a date value from the year, month, and day inputs.
Thanks for reading!!