Featured Database Articles

Working with SQL Server Date/Time Variables: Part Four - Date Math and Universal Time - Page 3

Universal Time

So what is
"Universal Time"? Universal Time is the local time at the Greenwich meridian,
which is longitude zero, or more historically called "Greenwich Mean Time".
"Universal Time" is most often used to record astronomical and weather
phenomena. Most civil usages of "Universal Time" are normally referred to as
"Coordinated Universal Time," which is normally abbreviated as UTC. UTC time is
five hours ahead of Eastern Standard Time.

GETUTCDATE Function

SQL Server
provides the GETUTCDATE function to get UTC time. The GETUTCDATE function
returns a datetime value that represents the current UTC time. The syntax for
the GETUTCDATE function is as follows:

GETUTCDATE()

The UTC
date and time is calculated based on the current time and time zone on the
local machine.

If you run
the following command on your server, the current UTC date will be displayed.

SELECT GETUTCDATE()

GETDATE Function

The GETDATE function provides a means to return the local
machine time (time for the time zone set on local server) in internal format.
The syntax for the GETDATE function is as follows:

GETDATE()

The GETDATE
function is useful to get the current time so you can use it in your
application.

Now it is
time to show a practical example of how to use both the GETUTCDATE and the
GETDATE functions. Let's use these functions along with the DATEDIFF function
to calculate the number of hours between a local SQL Server machine and
"Coordinated Universal Time". Here some code that does exactly that:

SELECT DATEDIFF(HH,GETUTCDATE(),GETDATE())

Conclusion

I hope this
article has shed some light on how to use and what you can do with the DATEADD,
and DATEDIFF functions. Also, you should now understand what a UTC date is and
how the GETUTCDATE can return a UTC date. In addition, you should also understand
how the GETDATE function would return the local server time in internal
datetime format.

I hope this
article and the first three (part 1,
part2,
and part3) in this series covered
most of what you might need to know to work with all of the different aspects
of SQL Server dates.