Bhavik Kama Bhavik Kama - 1 year ago 117
MySQL Question

How to Convert UTC Date To Local time Zone in MySql Select Query

I am using this Where Condition in One Of my query with MySql Database.My Problem is that i have one displaytime column in my table but that table column shows the data in UTC Time.and i want to convert that displaytime column in the Local Time how can i provide this facility from query itself.

I have goggled the things and by that i knew that something like

will work for that.but its not working for me.

Here is my query in which i need to convert displaytime to local time can anyone please guide me?

WHERE displaytime >= '2012-12-01 00:00:00'
AND displaytime <='2013-02-22 23:59:59'
AND ct.organizationId IN (
SELECT t.organizationId
FROM organization_ AS t
JOIN organization_ AS p ON t.treePath LIKE CONCAT(p.treePath, '%')
WHERE p.organizationId = 10707

enter image description here
SAmple DAta

enter image description here

Answer Source

SELECT CONVERT_TZ() will work for that.but its not working for me.

Why, what error do you get?

SELECT CONVERT_TZ(displaytime,'GMT','MET');

should work if your column type is timestamp, or date

Test how this works:

SELECT CONVERT_TZ(a_ad_display.displaytime,'+00:00','+04:00');

Check you timezone-table

SELECT * FROM mysql.time_zone;
SELECT * FROM mysql.time_zone_name;

If those tables are empty, you have not initialized your timezone tables. According to link above you can use mysql_tzinfo_to_sql program to load the Time Zone Tables. Please try this

shell> mysql_tzinfo_to_sql /usr/share/zoneinfo

or if not working read more:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download