Wednesday, April 27, 2011

SQL Server: Difference between GetDate() and GetUTCDate()

The difference between GETDATE() and GETUTCDATE() is time zone number of the SQL Server machine.

E.g.

DECLARE @local_time DATETIME;
DECLARE @gmt_time DATETIME;
SET @local_time = GETDATE();
SET @gmt_time = GETUTCDATE();
SELECT 'Server local time: '
   + CONVERT(VARCHAR(40),@local_time);
SELECT 'Server GMT time: '
   + CONVERT(VARCHAR(40),@gmt_time);
SELECT 'Server time zone: '
   + CONVERT(VARCHAR(40),
      DATEDIFF(hour,@gmt_time,@local_time));
GO


Output:
Server local time: Apr 26 2011 09:47PM
Server GMT time: Apr 26 2011  04:17PM
Server time zone: 5

GETDATE = Returns the day of the month for the specified date according to local time.

GETUTCDATE() = Returns the day (date) of the month in the specified date according to universal time.


 Reference: DP(http://dptechnicalblog.blogspot.com )

No comments:

Post a Comment