Difference UTC and GETUTCDATE()

Viewed 67

I'm having this issue

select getDATE() AT TIME ZONE 'utc'
SELECT GETUTCDATE()

output:

2021-06-08 12:50:19.260 +00:00
2021-06-08 11:50:19.260

Why does this happen? Basicaly I want to convert to UTC but it adds one hour. Current time in Portugal is now 12:50, so the first output should be 12:50

2 Answers

Firstly, to quote AT TIME ZONE (Transact-SQL):

Converts an inputdate to the corresponding datetimeoffset value in the target time zone. When inputdate is provided without offset information, the function applies the offset of the time zone assuming that inputdate is in the target time zone. If inputdate is provided as a datetimeoffset value, then AT TIME ZONE clause converts it into the target time zone using the time zone conversion rules.

GETDATE returns a datetime, it has no offset property. This means, per the quoted text above, it is assumed to be UTC when you set it the the UTC timezone. As you state you are in Portugal then you are currently UTC+1, and this means you (effectively) get a "UTC+1" UTC time.

GETUTCDATE, unsurprisingly, gives you the UTC time, and thus is correct.

If you want a datetimeoffset time with the current UTC time then use sysdatetimeoffset:

SELECT SYSDATETIMEOFFSET() AT TIME ZONE 'UTC';

This query:

select getDATE() AT TIME ZONE 'utc'

has two separate parts:

  • getDATE()

The documentation says:

Returns the current database system timestamp

  • AT TIME ZONE 'utc'

The documentation says:

Converts an inputdate to the corresponding datetimeoffset value in the target time zone. When inputdate is provided without offset information, the function applies the offset of the time zone assuming that inputdate is in the target time zone.

So the result from the local time is then double-adjusted again into the time zone offset


GETUTCDATE

This value represents the current UTC time (Coordinated Universal Time).

So if you used this with AT TIME ZONE 'UTC' then you would get the correct time

Related