SQL DATETIME2FROMPARTS function is one of the Date and Time Function used to return a datetime2 value from the user’s specified date and time parts. This function returns datetime2 data type value as output. The basic syntax of the DATETIME2FROMPARTS Function in SQL Server is as follows:
DATETIME2FROMPARTS (year, month, day, hour, minute, seconds, fractions, precision)
As you can see from the above syntax, this function accepts eight arguments to built date and time. Here, the Fraction datatype value will always depend upon the Precision. If the precision value is 5, then the value of the fraction must also be 5 (or less than 5); otherwise, SQL Server throws an error.
SQL DATETIME2FROMPARTS Function Example
In this example, we will show you the use of the DateTime2FromParts function
-- Example for SQL DATETIME2FROMPARTS Function SELECT DATETIME2FROMPARTS ( 2017, 12, 18, 15, 45, 09, 5, 1 ) AS Result SELECT DATETIME2FROMPARTS ( 2017, 12, 18, 15, 45, 09, 50, 2 ) AS Result SELECT DATETIME2FROMPARTS ( 2017, 12, 18, 15, 45, 09, 503, 3 ) AS Result -- Let me try the NULL values SELECT DATETIME2FROMPARTS ( 2017, 12, NULL, 15, 45, 09, 503, 3 ) AS Result -- Precision Value is greater than Fraction SELECT DATETIME2FROMPARTS ( 2017, 12, 18, 15, 45, 09, 503, 7 ) AS Result -- Precision Value is Les than Fraction. Invalid argument SELECT DATETIME2FROMPARTS ( 2017, 12, 18, 15, 45, 09, 503, 2 ) AS Result
As you can see, the last statement is throwing an error. This is because, Fraction value = 503, and Precision = 2 (does not match).
Let me show you the Date and Time Function result