SQL Server function DATEFROMPARTS for pre 2012 versions
SQL Server function for SQL 2000, 2005, 2008 and 2008R2 for same functionality as the 2012 function DATEFROMPARTS
About
This is a fairly simple function to replace the missing functionality of the DATEFROMPARTS function in SQL Server 2012 and later. It is a simple concatenation of the year, month and day into a date format, and returned as a date. Once built call it as you would any other function.
During testing for some additional DATETIME2 automated Easter dates, we found that some dates before 1000AD may error due to text length. We use a function to guarantee the length of each part of the date, so grab it from the link below.
This tends to be a crucial part of other date functions we use, and has been deployed in NHS and legal databases.
SQL
CREATE FUNCTION Dates.DateFromParts(@Year SMALLINT,@Month SMALLINT,@Day SMALLINT) RETURNS DATETIME2 AS BEGINDECLARE @Date DATETIME2=Dates.DatePad(@Year,4)+'-'+Dates.DatePad(@Month,2)+'-'+Dates.DatePad(@Day,2)RETURN @DateENDGO