Home >Database >Mysql Tutorial >How to Convert SQL Server DATETIMEOFFSET to UTC DATETIME?

How to Convert SQL Server DATETIMEOFFSET to UTC DATETIME?

Susan Sarandon
Susan SarandonOriginal
2025-01-01 02:58:10850browse

How to Convert SQL Server DATETIMEOFFSET to UTC DATETIME?

Converting SQL Server DATETIMEOFFSET to DATETIME in UTC

In SQL Server, you may encounter a scenario where you need to convert a DATETIMEOFFSET field to a DATETIME field while adjusting the time to compensate for the offset, effectively converting it to UTC.

To achieve this, you can utilize the CONVERT function as follows:

SELECT CONVERT(datetime2, @createdon, 1)

Here, @createdon represents your DATETIMEOFFSET field. The number 1 in the second parameter specifies that the conversion should be performed from the local time zone to UTC.

For example, let's consider a DATETIMEOFFSET value:

2008-12-19 17:30:09.0000000 +11:00

Upon applying the CONVERT function, it will be converted to:

2008-12-19 06:30:09.0000000

This corresponds to the same time in UTC.

Additionally, you can use the following method to explicitly set the offset to 00:00 when converting to DATETIMEOFFSET:

SELECT CONVERT(datetimeoffset, CONVERT(datetime2, @createdon, 1))

This will produce the following result:

2008-12-19 06:30:09.0000000 +00:00

Which indicates a DATETIMEOFFSET value in UTC.

The above is the detailed content of How to Convert SQL Server DATETIMEOFFSET to UTC DATETIME?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn