Link to home
Start Free TrialLog in
Avatar of JDCam
JDCam

asked on

SQL 2005 - Current time to GMT

Experts,

I use this code to select the current time.  I have been asked to convert it to GMT, so I need to add 4 hours. How can I do this

Select 
CONVERT(VARCHAR(5),GETDATE(), 108)

Open in new window

Avatar of Jim Horn
Jim Horn
Flag of United States of America image

If your question is just 'how can I add four hours', then use DATEADD()
SELECT DATEADD(hour, 4, GETDATE())

Open in new window

If your question is 'How can I convert any given GETDATE() to GMT', then that will imply some math, as we have to figure out what time zone you're in, plus based on that time zone (and local considerations such as US-Indiana or US-Arizona does not participate in Daylight Savings Time) whether or not you are in DST and therefore need to add/subtract an hour.
As long as Daylight Savings Time is not a problem (and if what you really want is UTC, not GMT), the following may help:

SELECT DATEADD(SECOND,-(DATEDIFF(SECOND,GETDATE(),GETUTCDATE()),GETDATE())

Open in new window

JimHorn solution will work, but only if GMT time is four hours ahead (which can be not always true).

If you want to get the current GMT time, just use the function GETUTCDATE():

SELECT GETUTCDATE();

Open in new window


Hope this helps. Regards.
Avatar of JDCam
JDCam

ASKER

all good.. but like my sample I need just the time in 24hr format
Define 'in 24hr format '.  This article is a real good cheat sheet for date formats.
ASKER CERTIFIED SOLUTION
Avatar of gplana
gplana
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial