13

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?

Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
Fred Fickleberry III
  • 2,439
  • 4
  • 34
  • 50

3 Answers3

12

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')
A_Sk
  • 4,532
  • 3
  • 27
  • 51
Jesse Petronio
  • 693
  • 5
  • 11
7

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

bas boek
  • 81
  • 1
  • 3
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')>
knkarthick24
  • 3,106
  • 15
  • 21