- Kategorien:
CONVERT_TIMEZONE¶
Konvertiert einen Zeitstempel in eine andere Zeitzone:
Syntax¶
CONVERT_TIMEZONE( <source_tz> , <target_tz> , <source_timestamp_ntz> )
CONVERT_TIMEZONE( <target_tz> , <source_timestamp> )
Argumente¶
source_tz
Zeichenfolge zur Angabe der Zeitzone für den Eingabezeitstempel. Ist für Zeitstempel ohne Zeitzone erforderlich (d. h. TIMESTAMP_NTZ).
target_tz
Zeichenfolge zur Angabe der Zeitzone, in die der Eingabezeitstempel konvertiert werden soll.
source_timestamp_ntz
Zeichenfolge, die für die Version mit drei Argumenten den zu konvertierenden Zeitstempel angibt (muss TIMESTAMP_NTZ sein).
source_timestamp
Zeichenfolge, die für die Version mit zwei Argumenten den zu konvertierenden Zeitstempel angibt (kann eine beliebige Zeitstempelvariante sein, einschließlich TIMESTAMP_NTZ).
Nutzungshinweise¶
Bei der Version mit drei Argumenten:
Die „Wanduhr“-Zeit im Ergebnis stellt den gleichen Zeitpunkt dar wie die Eingabe-„Wanduhr“ in der Eingabezeitzone, allerdings in der Zielzeitzone.
Der Rückgabewert ist immer vom Typ TIMESTAMP_NTZ.
Bei der Version mit zwei Argumenten:
Das Argument
source_timestamp
(Quellzeitstempel) soll üblicherweise die Zeitzone enthalten. Wenn der Wert vom Typ TIMESTAMP_TZ ist, wird die Zeitzone aus seinem Wert übernommen. Andernfalls wird die Zeitzone der aktuellen Sitzung verwendet.Der Rückgabewert ist immer vom Typ TIMESTAMP_TZ.
For
source_tz
andtarget_tz
, you can specify a time zone name or a link name from release 2021a of the IANA Time Zone Database (e.g.America/Los_Angeles
,Europe/London
,UTC
,Etc/GMT
, etc.).Bemerkung
Time zone names are case-sensitive and must be enclosed in single quotes (e.g.
'UTC'
).Snowflake does not support the majority of timezone abbreviations (e.g.
PDT
,EST
, etc.) because a given abbreviation might refer to one of several different time zones. For example,CST
might refer to Central Standard Time in North America (UTC-6), Cuba Standard Time (UTC-5), and China Standard Time (UTC+8).
Beispiele¶
ALTER SESSION SET timestamp_output_format = 'YYYY-MM-DD HH24:MI:SS';
-- Convert a "wallclock" time in Los Angeles to the matching "wallclock" time in New York
SELECT CONVERT_TIMEZONE('America/Los_Angeles', 'America/New_York', '2019-01-01 14:00:00'::timestamp_ntz) AS conv;
+-------------------------+
| CONV |
|-------------------------|
| 2019-01-01 17:00:00.000 |
+-------------------------+
-- Convert a "wallclock" time in Warsaw to the matching "wallclock" time in UTC
SELECT CONVERT_TIMEZONE('Europe/Warsaw', 'UTC', '2019-01-01 00:00:00'::timestamp_ntz) AS conv;
+-------------------------+
| CONV |
|-------------------------|
| 2018-12-31 23:00:00.000 |
+-------------------------+
ALTER SESSION UNSET timestamp_output_format;
-- Convert TIMESTAMP_TZ to a different time zone and include the time zone in the result
SELECT CONVERT_TIMEZONE('America/Los_Angeles', '2018-04-05 12:00:00 +02:00') AS time_in_la;
+-------------------------------+
| TIME_IN_LA |
|-------------------------------|
| 2018-04-05 03:00:00.000 -0700 |
+-------------------------------+
ALTER SESSION UNSET timestamp_output_format;
-- Show the current "wallclock" time in different time zones
SELECT
CURRENT_TIMESTAMP() AS now_in_la,
CONVERT_TIMEZONE('America/New_York', CURRENT_TIMESTAMP()) AS now_in_nyc,
CONVERT_TIMEZONE('Europe/Paris', CURRENT_TIMESTAMP()) AS now_in_paris,
CONVERT_TIMEZONE('Asia/Tokyo', CURRENT_TIMESTAMP()) AS now_in_tokyo;
+-------------------------------+-------------------------------+-------------------------------+-------------------------------+
| NOW_IN_LA | NOW_IN_NYC | NOW_IN_PARIS | NOW_IN_TOKYO |
|-------------------------------+-------------------------------+-------------------------------+-------------------------------|
| 2019-01-11 14:23:08.497 -0800 | 2019-01-11 17:23:08.497 -0500 | 2019-01-11 23:23:08.497 +0100 | 2019-01-12 07:23:08.497 +0900 |
+-------------------------------+-------------------------------+-------------------------------+-------------------------------+