How do I update a column's offset in SQL Server?
Asked Answered
S

3

16

I converted a table's DateTime field to DateTimeOffset, but now the offset is automatically set to +00:00.

I need to change all DateTimeOffset fields of this table to an offset of +1:00.

How can I do this in an update query?

Shillelagh answered 25/11, 2015 at 9:14 Comment(0)
S
13

You can use SWITCHOFFSET to change the offset. You will need to subtract the amount of hours though from the date if you don't want the date to change.

SELECT  SWITCHOFFSET(DATEADD(hh, -1, CAST (GETDATE() AS DATETIMEOFFSET)),
                         '+01:00')
Sejant answered 25/11, 2015 at 9:21 Comment(1)
Date and time should stay the same -- only the offset needs to change. Is this how I do it then?Shillelagh
M
8

You can use TODATETIMEOFFSET(datetime, '+01:00' ) This wont affect the datetime part.

Muddlehead answered 30/9, 2016 at 10:12 Comment(0)
J
2
DECLARE @t DATETIMEOFFSET

SELECT @t = Getdate()

SELECT Replace(@t, RIGHT(@t, 6), '+01:00') 

<update tablename set offsetfield = Replace(offsetfield, RIGHT(offsetfield, 6), '+01:00')>
Jolda answered 25/11, 2015 at 9:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.