We help IT Professionals succeed at work.

Transact SQL: Get the UTC TIME

Hi!

I've to write into a varchar(Requirements) the current UTC Time.

I tried to use the GetUtcDate(), but I've a strange result:

I've the server configured on a GMT+1 time, but if I do a GetUtcDate(), the date I've in response is 2 hours less than the hour of the computer. It should be only 1 hour less no?

Thank you!
Comment
Watch Question

Commented:
From the Books OnLine under GETUTCDATE():

The current UTC time is derived from the current local time and the time zone setting in the operating system of the computer on which the instance of Microsoft SQL Server is running.

This function does not return true UTC time, it uses the current server date/time and adds the timezone offset to UTC time, which I assume for you changed by 1 hour.

Moreover do you have the DayLight Saving configuration in which case the difference can happen as UTC time never changes with DayLight Savings.

Author

Commented:
Hum...

1. Okay so why it's 10:50 AM It respond 8:50 AM and not 9:50AM?(because of the daylight saving you think?)

2. What is the better way to get the true UTC TIME?
Top Expert 2010
Commented:
Hello Nargzul,

If you are on GMT+1, then I am guessing that you are somewhere in central Europe.  If that guess is correct,
then right now you are on Summer Time, and that would make the real offset between UTC and your current
local time to be +2.  (UTC does **not** adjust for DST/Summer Time.)

GETUTCDATE() retrieves the true system date/time as rendered in UTC, and as such requires no adjustment.

What is the problem?

Regards,

Patrick

Commented:
Use the dateadd() function. You will have to handle the logic for
the offset to UTC yourself, as SQL Server does not have any time zone
information.

Try this:

SELECT dateadd(mi, datediff(mi,GETUTCDATE() ,getdate())