"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Wednesday, November 2, 2011

Difference between GETDATE() and GETUTCDATE()

Hi *.*,
Playing with date-time is one of the biggest headache to me. :( . i faced a problem recently with that. Server is located at one place and client at another place  so what happened is the date doesn't change for 4.15 hrs!!!!.
Later v solved by adding GMT along with that.
Here i'm sharing the SQL script helped to get out of that.




Script comes here



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: '
   + CASE
        WHEN DATEDIFF(minute,@gmt_time,@local_time)>= 0 THEN
           '+'
        ELSE
           '-'
        END
   + RIGHT(CONVERT(VARCHAR(40),
      100+ABS(DATEDIFF(minute,@gmt_time,@local_time)/60)),
      2)
   + ':'
   + RIGHT(CONVERT(VARCHAR(40),
      100+ABS(DATEDIFF(minute,@gmt_time,@local_time)%60)),
      2);


Notes:
GETDATE()
GETUTCDATE()
DATEDIFF
ABS
RIGHT


If u had any trouble just ask, Happy to help u :) 
Stay Tune... 
Have a nice day... 'N happy Coding :)

No comments: