1

MySQL CONVERT_TZ returns null, if timezone value is in plus.

example:

select CONVERT_TZ(now(),SUBSTRING(timediff(now(),convert_tz(now(),@@session.time_zone,'+00:00')),1,5),'+00:00');

it returns perfect time 2015年12月16日 10:32:19 when the time zone is in minus value. The same is not working when the time zone value is in plus. For an example @@session.time_zone returns 05:30. If I concat plus manually, it is working. Please let me know, if any alternative solutions are available.

Manually concatenated code:

select CONVERT_TZ(now(),concat("+",SUBSTRING(timediff(now(),convert_tz(now(),@@session.time_zone,'+00:00')),1,5)),'+00:00');

Thanks in advance.

asked Dec 16, 2015 at 10:55

1 Answer 1

1

When the difference is positive, the SUBSTRING(timediff(now(),convert_tz(now(),@@session.time_zone,'+00:00')),1,5) part returns a positive values, thats without the +.

But CONVERT_TZ expects the offset to contain either + or -, so without it the value is not proper offset and the function returns NULL as specified.

Simply said - results of timediff are not proper timezone offsets.

You might use DATE_ADD instead, it allows for - too.

answered Dec 16, 2015 at 12:43
3
  • Thanks, Is there any other options to get proper timezone? Commented Dec 16, 2015 at 12:48
  • @Nisar I did not find any, but check the edited answer for an alternative. Commented Dec 16, 2015 at 12:50
  • Thanks again, Sorry! I already used this scenario which is creating some new issues using DATE_ADD and DATE_SUB. Commented Dec 16, 2015 at 12:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.