I have a field `opened_at` in MySQL table that stores datetimes in string format with different timezones:
2021-01-30T18:07:40+02:00
2021-01-31T01:32:40+05:00
2021-01-31T21:17:40+03:00
etc.
I need to SELECT
rows from this table with values in `opened_at` within the specified local date of local timezone, for example 2021-01-31 +02:00
.
The problem is that when I cast `opened_at` to datetime I loose timezone offsets, so I can't do correct comparison. For example:
mysql> SELECT CAST("2021-01-30T18:07:40+02:00" AS DATETIME) = CAST("2021-01-30T18:07:40+00:00" AS DATETIME);
+-----------------------------------------------------------------------------------------------+
| CAST("2021-01-30T18:07:40+02:00" AS DATETIME) = CAST("2021-01-30T18:07:40+00:00" AS DATETIME) |
+-----------------------------------------------------------------------------------------------+
| 1 |
+-----------------------------------------------------------------------------------------------+
But "2021-01-30T18:07:40+02:00"
is not equal to "2021-01-30T18:07:40+00:00"
. There's a two-hour difference between them!
I've read this, but the way in winning answer is too hard for SELECT from big number of rows.
Is there some quick and processor-efficient way to cast strings with times in different timezones for further comparison with some datetime interval?
opened_at
field is not a database field, but one of the elements of the JSON type field (`checkbox_shifts`.`data`->>'$.opened_at' AS `opened_at`), and this JSON absolutely cannot be changed.TIMESTAMP
instead ofDATETIME
. (Too late now.)