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

How to Convert SQL Server DATETIMEOFFSET to UTC DATETIME?

Susan Sarandon
Release: 2025-01-01 02:58:10
Original
856 people have browsed it

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)
Copy after login

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
Copy after login

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

2008-12-19 06:30:09.0000000
Copy after login

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))
Copy after login

This will produce the following result:

2008-12-19 06:30:09.0000000 +00:00
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template