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

LVL 1
JDCamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.
0
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
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

0
gplanaCommented:
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.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

JDCamAuthor Commented:
all good.. but like my sample I need just the time in 24hr format
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Define 'in 24hr format '.  This article is a real good cheat sheet for date formats.
0
gplanaCommented:
Try this:

SELECT CONVERT(VARCHAR(5),GETUTCDATE(),108);

Open in new window


Hope this helps. Regards.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.