Introduction to DateTime2FromParts

The SQL DateTime2FromParts function was introduced in the SQL server in 2012. It is a function used to construct a datetime2 value from a given individual date and various time segments. If you are not familiar datetime2 data type then go through the following points for better understanding. This function is mainly used when we have separate values of the exact time segments and we want to combine them to form a single datetime2 value.

  • The datetime2 is a date and time data type in SQL server that has a higher precision than the older DATETIME type.
  • It stores dates and times with up to 100 nanoseconds of precision.
  • It takes 8 bytes of storage space as compared to normal DateTime which takes 4 bytes.
  • It follows the following format to display the date and time YYYY-MM-DD HH:MM: SS.FFFFFFF.
  • It supports a date range from January 1, 1753 to December 31 9999.

Syntax:

DATETIME2FROMPARTS ( year, month, day, hour, minute, seconds, fractions, precision )

Arguments:

The function accepts 8 arguments. Each of the argument is described below:

  • Year: An integer value that specifies a year. Can be a 4-digit value like 2024.
  • Month: An integer value between 1 and 12 that specifies a month.
  • Day: An integer value between 1 and 31 that specifies a day.
  • Hour: An integer value between 0 and 23 that specifies the hours.
  • Minute: An integer value between 0 and 59 that specifies the minutes.
  • Seconds: An integer value between 0 and 59 that specifies the seconds.
  • Fractions: An integer value that specifies a fractional seconds value.
  • Precision: An integer value between 0 and 7 that specifies the precision of the datetime2 value that DATETIME2FROMPARTS will return.
  • The return type of the function is a datetime2 value.

SQL Server DATETIME2FROMPARTS Function

In this article, we are going to explore the DateTime2FromParts function in SQL server. This function is very useful in such situations where we need to construct a datetime value using individual parts like year, month, day, hour, and minutes. Deep dive into this article to understand the use of this function with proper explanations along with examples.

Similar Reads

Introduction to DateTime2FromParts

The SQL DateTime2FromParts function was introduced in the SQL server in 2012. It is a function used to construct a datetime2 value from a given individual date and various time segments. If you are not familiar datetime2 data type then go through the following points for better understanding. This function is mainly used when we have separate values of the exact time segments and we want to combine them to form a single datetime2 value....

Examples of DateTime2FromParts function in SQL Server 2012

Now, let us consider some practical applications of Datetime2fromparts function for a better understanding about of the working of the function....

Conclusion

In this article we have learned about the DATETIME2FROMPARTS function in SQL servers and its application in various practical use cases. Whenever we have separate time and date fragments and we want to generate a single datetime2 value we can use this function. However , it is important to note that if any of the arguments passed to the function are invalid then the function will generate an error so make sure that your arguments are valid before executing the function....