converting a datetime value's timezone in a query

Hi,

I've got a simple query "SELECT * FROM table1" and one of the columns in table1 holds a datetime value. I want to convert this datetime value to a different time zone (presumably using CONVERT_TZ??). Can someone help me get the correct syntax for using this function within an SQL SELECT query...

Thanks,

JJ
jm_jacksonAsked:
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.

tdterryCommented:

select convert_tz(my_time_field, 'DB time zone', 'desired time zone') from table1;

You will of course need to know the time zone that the data is stored in and the time zone of the user to whom your displaying the data.


From the manual:

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
        -> '2004-01-01 13:00:00'
mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');
        -> '2004-01-01 22:00:00'


0
jm_jacksonAuthor Commented:
Thanks,

I'm running into a problem as I get an error message when trying to execte the following query:

SELECT convert_tz(click_time, '+00:00','+05:00') FROM clicks

(where click_time is the datetime column)

The error I get is:

#1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near '( click_time , '+00:00' , '+05:00' ) FROM table1'
0
jm_jacksonAuthor Commented:
Incidentally, I have MySQL 4.0.25-standard running...
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

tdterryCommented:
Oh, CONVERT_TZ was added in 4.1.3.

You can use FROM_UNIXTIME and UNIX_TIMESTAMP to convert to integers which represent seconds.  There are 3600 seconds in an hour, so all you have to do is add or subtract the correct number of seconds inbetween.  

Note: there may be a loss when converting between UNIX timestamp and back to SQL datetime.  This is documented, but I don't know specifically what cases cause it.  Probably won't a problem unless you were converting back and forth and compounding the conversion multiple times.

For + 5 hours,

SELECT FROM_UNIXTIME( UNIX_TIMESTAMP(click_time), + 18000)
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
tdterryCommented:
oops, typo.  Ignore that extra comma before the +
0
jm_jacksonAuthor Commented:
Brilliant. Thanks tdterry!
0
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
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.