SQL CONVERT_TZ function
Converts a datetime from one time zone to another, by named zone or by ±HH:MM offsets.
- Returns
datetime- In this category
- 106 of 121
语法
CONVERT_TZ(dt, from_zone, to_zone)
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | Different name or syntax | d AT TIME ZONE '...' chained twice |
| PostgreSQL | Different name or syntax | d::timestamptz AT TIME ZONE '...' |
| MySQL | 是 | Supported under this name. |
| SQLite | Not built in | none; utc/localtime modifiers only |
| Oracle | Different name or syntax | FROM_TZ(dt, 'zone') AT TIME ZONE 'other' |
示例
MySQL
SELECT CONVERT_TZ('2026-08-01 14:30:00', 'UTC', 'Europe/Paris'); -- 2026-08-01 16:30:00 (needs tz tables)
MySQL
SELECT CONVERT_TZ(created_at, @@session.time_zone, '+00:00') FROM logs;
SQL Server
SELECT d AT TIME ZONE 'UTC' AT TIME ZONE 'Central European Standard Time' FROM logs;
值得了解
- Named zones need the time zone tables loaded (mysql.time_zone_name etc.); on many installs they are empty and CONVERT_TZ(dt, 'UTC', 'Europe/Paris') silently returns NULL while offsets like '+02:00' work.
- Offsets ignore DST: converting with '+01:00' is right in winter and wrong in summer; load the zone tables and use IANA names for wall-clock correctness.
- Input and output are naive datetimes. MySQL has no type that stores a zone: DATETIME is naive, and TIMESTAMP converts from the session time_zone to UTC on write and back on read without remembering the original zone.
Questions about CONVERT_TZ
Why does CONVERT_TZ return NULL?
Usually a named zone that is missing from the mysql.time_zone tables, which are empty on many fresh installs. Load them with mysql_tzinfo_to_sql, or use fixed offsets like '+02:00' and accept the DST error.
What is the PostgreSQL equivalent?
dt AT TIME ZONE 'from' AT TIME ZONE 'to' (interpret then render), or CAST to timestamptz and render in the target zone.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.