// dates & times reference

SQL FROM_TZ function

Returns a TIMESTAMP WITH TIME ZONE built from a plain timestamp plus a zone name or offset, letting session-independent conversions follow.

Categoría
Dates & times
Returns
TIMESTAMP WITH TIME ZONE
In this category
36 of 121

Sintaxis

FROM_TZ(ts, 'UTC')
FROM_TZ(ts, 'Europe/London')
FROM_TZ(ts, '-05:00')

Each block is a separate form. The support table and the examples show which engine takes which.

Soporte del motor

MotorSupportNotas
SQL Server Different name or syntax TODATETIMEOFFSET / AT TIME ZONE
PostgreSQL Different name or syntax ts AT TIME ZONE 'zone'
MySQL Not built in no zone-aware type; CONVERT_TZ instead
SQLite Not built in no zone-aware type
Oracle Sí Supported under this name.

Ejemplos

Oracle

SELECT FROM_TZ(TIMESTAMP '2024-01-15 09:30:00', 'UTC') AS utc_ts FROM dual;

Oracle

SELECT FROM_TZ(ts, 'Europe/London') AT TIME ZONE 'UTC' AS utc_ts
FROM events;

PostgreSQL

SELECT '2024-01-15 09:30:00'::timestamp AT TIME ZONE 'UTC';

Conviene saberlo

  • A region name like 'America/New_York' applies whichever offset was in force on that date, DST included; a fixed '-05:00' never changes.
  • An unknown region raises ORA-01882: either a typo, or a zone newer than the server's time-zone file version.
  • FROM_TZ is not the same as ts AT TIME ZONE zone. On a plain TIMESTAMP, AT TIME ZONE first assumes the session zone and then converts, so 09:30 can come back as 14:30 UTC.

Questions about FROM_TZ

What is the difference between FROM_TZ and CAST?

CAST to TIMESTAMP WITH TIME ZONE assumes the session zone; FROM_TZ names the zone explicitly, which is reproducible across sessions.

Why ORA-01882 on a correct-looking zone?

The server's time-zone file does not list it. Check the name against V$TIMEZONE_NAMES and the file version in V$TIMEZONE_FILE.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

// fewer lookups

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.