Home > Database > Mysql Tutorial > How to Convert SQL Server 2008 DateTimeOffset to DateTime with Offset Adjustment?

How to Convert SQL Server 2008 DateTimeOffset to DateTime with Offset Adjustment?

Susan Sarandon
Release: 2025-01-03 16:33:42
Original
780 people have browsed it

How to Convert SQL Server 2008 DateTimeOffset to DateTime with Offset Adjustment?

Converting SQL Server 2008 DateTimeOffset to DateTime with Offset Adjustment

When working with databases, it may be necessary to convert data types to ensure compatibility or facilitate data analysis. One common conversion involves changing a DATETIMEOFFSET field to a DATETIME field while recalculating the time based on the specified offset. This guide provides the steps to accomplish this conversion effectively.

Conversion Process

The conversion from DATETIMEOFFSET to DATETIME involves the following procedures:

  1. Use CONVERT Function: Use the CONVERT function to transform the DATETIMEOFFSET value to a DATETIME2 data type with style 1, which preserves the date and time while converting to UTC.
CONVERT(datetime2, @created_on, 1)
Copy after login
  1. Convert Back to DATETIMEOFFSET: To retain the adjusted time in a DATETIMEOFFSET format, convert the DATETIME2 value back to DATETIMEOFFSET and specify an offset of 00:00.
CONVERT(datetimeoffset, CONVERT(datetime2, @created_on, 1))
Copy after login

Example Conversion

Let's consider the following DATETIMEOFFSET value:

@created_on = '2008-12-19 17:30:09.1234567 +11:00'
Copy after login

Converting this value to DATETIME using the CONVERT and CONVERT functions results in the following values:

CONVERT(datetime2, @created_on, 1) = '2008-12-19 06:30:09.12'
CONVERT(datetimeoffset, CONVERT(datetime2, @created_on, 1)) = '2008-12-19 06:30:09.1234567 +00:00'
Copy after login

As can be seen in the example, the conversion process recalculates the time by taking into account the offset ( 11:00) and converts the value to UTC ( 00:00).

The above is the detailed content of How to Convert SQL Server 2008 DateTimeOffset to DateTime with Offset Adjustment?. 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