SQL Server TODATETIMEOFFSET() Function

The TODATETIMEOFFSET() function is a date and time function introduced in SQL Server 2008. It allows combining a datetime value with a time zone offset value to create a DateTimeOffset value. The DateTimeOffset type is a structure with a date and time and a time zone offset, which can represent a date and time in any time zone in the world. Therefore, the TODATETIMEOFFSET() function is one of the commonly used functions for handling multi-time zone applications.

Syntax

TODATETIMEOFFSET ( expression , time_zone )

Parameter description:

  • expression: The date and time value to be converted to the DateTimeOffset type. Required.
  • time_zone: A string value or offset value representing a time zone offset. Required. The offset can be an integer value representing hours, minutes, or seconds from UTC.

Use Cases

In a multi-time zone application, different users may be in different time zones, and their local times need to be stored and displayed in the application. This is where the TODATETIMEOFFSET() function comes in handy. We can use it to convert local time and time zone offset values into DateTimeOffset values, which can display date and time correctly in different time zones.

Examples

Example 1

Assume that we have a table containing date and time values and time zone offsets, and all date and time values are based on New York City time.

datetime time_zone
2022-03-10 10:00:00 -05:00
2022-03-10 14:00:00 -05:00

We want to convert these date and time values to DateTimeOffset values so that they can be displayed correctly in different time zones. Suppose we want to convert these values to DateTimeOffset values based on UTC. The following SQL query can perform this conversion:

SELECT TODATETIMEOFFSET(datetime, time_zone) AS datetimeoffset_utc
FROM table_name;

The query results are as follows:

datetimeoffset_utc
2022-03-10 15:00:00.0000000 +00:00
2022-03-10 19:00:00.0000000 +00:00

Example 2

Assume that we have a table containing date and time values and time zone offsets, and all date and time values are based on New York City time.

datetime time_zone
2022-03-10 10:00:00 -05:00
2022-03-10 14:00:00 -05:00

We want to convert these date and time values to DateTimeOffset values so that they can be displayed correctly in different time zones. Suppose we want to convert these values to UTC time:

SELECT SWITCHOFFSET(datetime, '+00:00') AS utc_datetime
FROM mytable;

After executing the above statement, we get the following results:

utc_datetime
2022-03-10 15:00:00.0000000 +00:00
2022-03-10 19:00:00.0000000 +00:00

Now we can use the UPDATE statement to update the converted UTC time to the table:

UPDATE mytable
SET datetime = SWITCHOFFSET(datetime, '+00:00');

After executing the above statement, our table will become:

datetime
2022-03-10 15:00:00.0000000 +00:00
2022-03-10 19:00:00.0000000 +00:00

In this way, we have successfully converted the date and time in the table to UTC time and updated them in the table.

Conclusion

The SWITCHOFFSET() function is a function in SQL Server used to convert time zones for date and time values. It can convert the specified date and time value from one time zone to another and return the converted result. Using this function, we can easily convert date and time values in different time zones to the time zone we need or convert them to UTC time for storage or comparison.