SQL - DATETIMEOFFSETFROMPARTS() Function



The SQL DATETIMEOFFSETFROMPARTS() function enables you to extract a datetimeoffset value from each of a date's component parts. It specifically returns a datetimeoffset value for the given time and date, along with the offsets and precision.

DATETIMEOFFSETFROMPARTS is one of the crucial SQL server operations used when making date and time from offset and precision. Based on the given date and time component inputs, a value is returned. Every argument is necessary. Null is returned if any is null.

Syntax

Following is the syntax for SQL DATETIMEOFFSETFROMPARTS() function −

DATETIMEOFFSETFROMPARTS ( year, month, day, hour, minute, seconds, fractions, hour_offset, minute_offset, precision )

Parameters

This function accepts ten parameter as discussed below −

  • year − integer expression that indicates year.

  • month − integer expression that indicates month.

  • day − integer expression that indicates day.

  • hour − integer expression that indicates hours.

  • minute − integer expression that indicates minutes.

  • seconds − integer expression that indicates seconds.

  • fractions − integer expression that indicates a fractional seconds value.

  • hour_offset − integer expression that indicates the hour portion of time zone offset.

  • minute_offset − integer expression that indicates the minute portion of time zone offset.

  • precision − integer value that indicates the precision of the datetimeoffset value that DATETIMEOFFSETFROMPARTS will return.

Example

Here, we are using theDATETIMEOFFSETFROMPARTS() function along with date and time arguments to construct DATETIMEOFFSET by using the following query −

SELECT DATETIMEOFFSETFROMPARTS(2023, 02, 21, 12, 28, 30, 5000, 10, 30, 4) AS Result;

Output

When we execute the above query, the output is obtained as follows −

+---------------------------------+
| Result                          |
+---------------------------------+
| 2023-02-21 12:28:30.5000 +10:30 |
+---------------------------------+

Example

Let us look into in the another scenario, where we are going to use the null value for the date and checking the result by running the following query −

SELECT DATETIMEOFFSETFROMPARTS(2023, 02, NULL, 12, 28, 30, 5000, 10, 30, 4) AS Result;

Output

When the query, gets executed it will generate the output as shown below −

+-------+
| Result|
+-------+
| NULL  |
+-------+

Example

In the following example, we are going to use the invalid arguments which causes the DATETIME2FROMPARTS() function returns an error, let's check this by running the following query −

SELECT DATETIMEOFFSETFROMPARTS(2023, 22, 21, 12, 28, 30, 5000, 10, 30, 4) AS Result;

Output

On executing the above query, the output is displayed as follows−

Cannot construct data type datetimeoffset, some of the arguments have values which are not valid.

Example

Let us look into the another scenario, where we are not going to specify the total number of arguments and checking the result by running the query −

SELECT DATETIMEOFFSETFROMPARTS(2023, 22, 21) AS Result;

Output

When the query, gets executed it will generate the output as shown below −

The datetimeoffsetfromparts function requires 10 argument(s).

Example

Here, we are going to mention the last precision value to be NULL and checking the result by running the following query −

SELECT DATETIMEOFFSETFROMPARTS(2023, 22, 21, 12, 28, 30, 5000, 10, 30, NULL) AS Result;

Output

The output for the above query is produced as given below −

Scale argument is not valid. Valid expressions for data type datetimeoffset scale argument are integer constants and integer constant expressions.

Example

In the following example we are going to mention the precision value greater than fractional and checking the result by using the following query −

SELECT DATETIMEOFFSETFROMPARTS(2023, 02, 21, 12, 28, 30, 363, 10, 30, 7) AS Result;

Output

When the query gets executed, it will generate an output as shown below −

+------------------------------------+
| Result                             |
+------------------------------------+
| 2023-02-21 12:28:30.0000363 +10:30 |
+------------------------------------+

Example

Let us consider another scenario, where we are going to use precision value less than fractional and checking the result by using the following query −

SELECT DATETIMEOFFSETFROMPARTS(2023, 02, 21, 12, 28, 30, 363, 10, 30, 2) AS Result;

Output

On running the above query, it will generate an output as shown below −

Cannot construct data type datetimeoffset, some of the arguments have values which are not valid.
sql-date-functions.htm
Advertisements