As a database programmer sometime you may get values of day, month and year in integer format. You may need to use these values to generate a date in date datatype. For this purpose you can use the built-in function DATEFROMPARTS. This function was introduced in SQL Server 2012. This function accepts day, month and year values in integer and returns a date. Let’s see how to use DATEFROMPARTS.
DATEFROMPARTS ( <year>, <month>, <day> ) Where: <year> = Years in integer, <month> = Months in integer, <day> = Days in integer
DECLARE @year INT; DECLARE @month INT; DECLARE @day INT; SET @year = 2016 SET @month = 7 SET @day = 25 SELECT DATEFROMPARTS(@year, @month, @day); GO /* Result */ 2016-07-25
- About DATEFROMPARTS at Microsoft Docs.