How can I convert a Sql Server 2008 DateTimeOffset to a DateTime

后端 未结 6 1405
独厮守ぢ
独厮守ぢ 2020-11-27 05:15

I\'m hoping to convert a table which has a DATETIMEOFFSET field, down to a DATETIME field BUT recalculates the time by taking notice of the offset.

6条回答
  •  粉色の甜心
    2020-11-27 05:58

    In order to account for daylight savings time, I used the following:

    CONVERT(
      DateTime, 
      SWITCHOFFSET(
        CONVERT(
          DateTimeOffset, 
          CONVERT(
            DateTime, 
            [time_stamp_end_of_interval], 
            120
          )
        ),
        DATENAME(
          TzOffset, 
          CONVERT(
            DateTime, 
            [time_stamp_end_of_interval], 
            120
          ) AT TIME ZONE 'Pacific Standard Time'
        )
      )
    )
    AS GOOD_PST
    

    Note: time_stamp_end_of_interval is a varchar

提交回复
热议问题