Back to Materialize

TIMEZONE and AT TIME ZONE functions

doc/user/content/sql/functions/timezone-and-at-time-zone.md

1232.3 KB
Original Source

TIMEZONE and AT TIME ZONE convert a timestamp or a timestamptz to a different time zone.

Known limitation: You must explicitly cast the type for the time zone.

Signatures

{{% include-syntax file="examples/sql_functions/timezone" example="syntax-timezone" %}}

{{% include-syntax file="examples/sql_functions/timezone" example="syntax-at-time-zone" %}}

ParameterTypeDescription
zonetextThe target time zone.
typetext or numericThe datatype in which the time zone is expressed
timestamptimestampThe timestamp without time zone.
timestamptztimestamptzThe timestamp with time zone.

Return values

TIMEZONE and AT TIME ZONE return timestamp if the input is timestamptz, and timestamptz if the input is timestamp.

Note: timestamp and timestamptz always store data in UTC, even if the date is returned as the local time.

Examples

Convert timestamp to another time zone, returned as UTC with offset

mzsql
SELECT TIMESTAMP '2020-12-21 18:53:49' AT TIME ZONE 'America/New_York'::text;
        timezone
------------------------
2020-12-21 23:53:49+00
(1 row)
mzsql
SELECT TIMEZONE('America/New_York'::text,'2020-12-21 18:53:49');
        timezone
------------------------
2020-12-21 23:53:49+00
(1 row)

Convert timestamp to another time zone, returned as specified local time

mzsql
SELECT TIMESTAMPTZ '2020-12-21 18:53:49+08' AT TIME ZONE 'America/New_York'::text;
        timezone
------------------------
2020-12-21 05:53:49
(1 row)
mzsql
SELECT TIMEZONE ('America/New_York'::text,'2020-12-21 18:53:49+08');
        timezone
------------------------
2020-12-21 05:53:49
(1 row)