问题
I need to SELECT a ISO 8601 Date.
DATE_FORMAT(date, '%Y-%m-%dT%TZ')
This produces something like
2013-11-13T15:47:530Z
But I need it with the offset instead of Z value:
2013-11-13T15:47:53+02:00
How can I do this with plain MySQL ?
回答1:
You need to store the timezone as an extra column in DB. I do not know any DB that stores datetime with timezone/offset.
Or store the date as string in ISO 8601 format with offset..
Edit: I stand somewhat corrected, with some newer databases it is possible!
- Postgresql Date/Time Types, which indeed may include timezone.
- Datatype: "timestamp [ (p) ] with time zone" only for input and output, it is stored in UTC.
- Datatype: "time with time zone", only time, stores timezone offset. Since version 8.
- ORACLE Datetime Datatypes and Time Zone Support.
- Datatype: "TIMESTAMP WITH TIME ZONE", stores timezone offset. Since version 9i.
- Datatype: "TIMESTAMP WITH TIME ZONE", stores timezone offset or timezone name. Since version 10g.
- See also Oracle TIMESTAMP WITH TIMEZONE named zone vs offset.
- MicroSoft SQL Server 2008
- Datatype: "datetimeoffset", stores timezone offset.
- Sybase
- Datatype: "TIMESTAMP WITH TIME ZONE", stores timezone offset.
- Support for TIMESTAMP WITH TIME ZONE is optional SQL language feature F411 of the SQL/2008 standard.
Seems to actually be somewhat standard SQL99.
Not for Mysql. Version 5.6.
- Nor for SQLite. Version 3.
I do find consolation in the fact that the correction came from myself ;-)
回答2:
his snippet should work for iso 8601 without milis, zulu time is also not supported:
select IF(
LENGTH(value) < 23,
STR_TO_DATE(value,'%Y-%m-%dT%TZ'),
CASE SUBSTRING(value from 20 for 1)
WHEN '+' THEN
DATE_ADD(
STR_TO_DATE(SUBSTRING(value from 1 for 19),'%Y-%m-%dT%TZ'),
INTERVAL SUBSTRING(value from 21 for 2) HOUR)
WHEN '-' THEN
DATE_SUB(
STR_TO_DATE(SUBSTRING(value from 1 for 19),'%Y-%m-%dT%TZ'),
INTERVAL SUBSTRING(value from 21 for 2) HOUR
)
END
)
from THE_NAMESPACE.THE_TABLE.THE_COLUMN as value;
来源:https://stackoverflow.com/questions/19977408/mysql-iso-8601-date-with-timezone-offset-instead-of-z-value