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:
CONVERT(datetime2, @created_on, 1)
CONVERT(datetimeoffset, CONVERT(datetime2, @created_on, 1))
Example Conversion
Let's consider the following DATETIMEOFFSET value:
@created_on = '2008-12-19 17:30:09.1234567 +11:00'
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'
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!