Introduction
SQL Server 2012 introduced the seven new date functions: DATEFROMPARTS, DATETIME2FROMPARTS, DATETIMEFROMPARTS, DATETIMEOFFSETFROMPARTS, EOMONTH, SMALLDATETIMEFROMPARTS and TIMEFROMPARTS.
DATEFROMPARTS
The DATEFROMPART function returns a date value from a given year, month and day. If the parameter value(s) are not valid then it throws an error. If any argument is NULL then the result is also "NULL".
Syntax
DATEFROMPARTS ( year, month, day )
Argument / parameter
- Year: Integer value for specifying a year.
- Month: Integer value for specifying month (from 1 to 12).
- Day: Integer value for specifying day.
Example
SELECT DATEFROMPART(2013,9,26)
--Output
-- 09/26/2013
DATETIMEFROMPARTS
The DATETIMEFROMPARTS returns a DATETIME value from a given date and time. If the parameter value(s) are not valid then it throws an error. If any argument is NULL then the result is also "NULL".
Syntax
DATETIMEFROMPARTS ( Year, Month, Day, Hour, Minute, Seconds)
Argument / parameter
Arguments: Year, Month and Day are the same as for the DATEFROMPARTS function.
-
Hour: Integer value for specifying hours.
-
Minute: Integer value for specifying minutes.
-
Seconds: Integer value for specifying seconds.
Example
SELECT DATETIMEFROMPARTS ( 2013, 9, 26, 9, 59, 59)
SELECT DATETIMEFROMPARTS ( 2013, 9, 26, 9, 59, 59)
--Output
-- 2013-09-26 09:59:59
DATETIME2FROMPARTS
DATETIME2FROMPARTS returns a DATETIME2 value from a given date and time with specified precision. If any argument is invalid then this function raises an error. If any argument is null then this function returns "NULL".
Syntax
DATETIME2FROMPARTS ( Year, Month, Day, Hour, Minute, Seconds, Fractions, Precision )
Argument / parameter
Arguments Year, Month, Day, Hour, Minute and Seconds are same as DATETIMEFROMPARTS
-
Fractions: Integer value for specifying fractions.
-
Precision: This is Integer value that specifies the precision of the datetime2 value to be returned.
Example
SELECT DATETIME2FROMPARTS ( 2013, 9, 26, 9, 59, 59, 0, 0 )
--Output
-- 2013-09-26 09:59:59.0000000
The fraction argument depends on precision argument. If the precision value is zero then fraction value must be zero, otherwise this function raised error. If precision argument is NULL then this function raised error. For example if precision value is 3 then fraction represent s a value in milliseconds.
SELECT DATETIME2FROMPARTS ( 2013, 9, 26, 9, 59, 59, 50, 2 )
--Output
-- 2013-09-26 09:59:59.50
DATETIMEOFFSETFROMPARTS
The DATETIMEOFFSETFROMPARTS function returns a DATETIMEOFFSET from given date and time with specified offsets and precision. This function raised error if any supplied argument is invalid. If any required argument is null then this function returns NULL.
Syntax
DATETIMEOFFSETFROMPARTS ( Year, Month, Day, Hour, Minute, Seconds, Fractions, Hour Offset, Minute Offset, Precision )
Argument / parameter
Arguments Year, Month, Day, Hour, Minute, Seconds, Fractions and Precision are same as DATETIME2FROMPARTS function.

Join the conversation! Your thoughts help the community grow.