converting a datetime value's timezone in a query

Posted on 2006-04-10
Last Modified: 2012-06-27

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...


Question by:jm_jackson
    LVL 5

    Expert Comment


    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:

    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'


    Author Comment


    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'

    Author Comment

    Incidentally, I have MySQL 4.0.25-standard running...
    LVL 5

    Accepted Solution

    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)
    LVL 5

    Expert Comment

    oops, typo.  Ignore that extra comma before the +

    Author Comment

    Brilliant. Thanks tdterry!

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now