Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Data in GMT

Author  Topic 

youruseridistoxic
Yak Posting Veteran

65 Posts

Posted - 2008-10-27 : 09:08:59
Good morning,

I have a datetime column which records data in GMT. How can I convert this data in a query to use the local time (in my case, EST)?

Thank you!

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2008-10-27 : 09:16:43
If the difference in hours between GMT and EST is constant you can use the DATEADD built in function:

SELECT DATEADD(hh, -5, mydatecolumn)

- Lumbago
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2008-10-27 : 09:19:14
...or if this is supposed to be displayed dynamically for users across the planet you need to store somewhere (usually in a user profile table) each users offset to GMT and then select like this:

SELECT UserID, DATEADD(hh, CustomOffsetColumn, mydatecolumn) FROM users

- Lumbago
Go to Top of Page

youruseridistoxic
Yak Posting Veteran

65 Posts

Posted - 2008-10-27 : 09:26:18
Excellent, thank you both!
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2008-10-27 : 09:31:49
Thank you...and thank you

- Lumbago
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-10-27 : 12:08:36
You can also make use of the GETUTCDATE() to get the correct offset taking into account daylight savings:

SELECT DATEDIFF(HOUR, GETUTCDATE(), GETDATE())
Go to Top of Page
   

- Advertisement -