Converts a VARCHAR date string into a TIMESTAMP value in the time zone of the Flink runtime environment.
Syntax
TIMESTAMP TO_TIMESTAMP_TZ(VARCHAR date, VARCHAR timezone)
TIMESTAMP TO_TIMESTAMP_TZ(VARCHAR date, VARCHAR format, VARCHAR timezone)
The first version parses date using the default format yyyy-MM-dd HH:mm:ss and interprets it in the specified timezone.
The second version lets you specify a custom format for the input date string before applying the timezone conversion.
Input parameters
|
Parameter |
Data type |
Description |
|
|
VARCHAR |
The input date string. |
|
|
VARCHAR |
The format of the input date string. Default value: |
|
|
VARCHAR |
The time zone of the input date, specified as an IANA time zone name (for example, |
Usage notes
-
Use IANA time zone names for the
timezoneparameter. If a non-standard value is provided, the parameter defaults to UTC. -
The
formatparameter applies only to the second overload. The default format isyyyy-MM-dd HH:mm:ss.
Example
The following example assumes the Flink runtime environment runs in the Asia/Shanghai time zone (UTC+8).
Test data — Table T1
|
timestamp1 (VARCHAR) |
format1 (VARCHAR) |
timezone1 (VARCHAR) |
|
1970-01-01 00:00:00 |
yyyy-MM-dd HH:mm:ss |
/ |
|
timestamp2 (VARCHAR) |
format2 (VARCHAR) |
timezone2 (VARCHAR) |
|
1970-01-01 00:00:00 |
yyyy-MM-dd HH:mm:ss |
Asia/Shanghai |
|
timestamp3 (VARCHAR) |
format3 (VARCHAR) |
timezone3 (VARCHAR) |
|
1970-01-01 00:00:00 |
yyyy-MM-dd HH:mm:ss |
America/New_York |
Test statement
SELECT
TO_TIMESTAMP_TZ(timestamp1, timezone1) AS var1, -- returns 1970-01-01 08:00:00
TO_TIMESTAMP_TZ(timestamp2, format2, timezone2) AS var2, -- returns 1970-01-01 00:00:00
TO_TIMESTAMP_TZ(timestamp3, format3, timezone3) AS var3 -- returns 1970-01-01 13:00:00
FROM T1;
Test result
|
var1 (VARCHAR) |
var2 (VARCHAR) |
var3 (VARCHAR) |
|
1970-01-01 08:00:00 |
1970-01-01 00:00:00 |
1970-01-01 13:00:00 |
Result explanation
-
var1:
timezone1is/, which is a non-standard value. The function falls back to UTC. The Flink runtime inAsia/Shanghai(UTC+8) displays the UTC epoch1970-01-01 00:00:00as1970-01-01 08:00:00. -
var2:
timezone2isAsia/Shanghai, which matches the Flink runtime time zone. The input is already inAsia/Shanghaitime, so the TIMESTAMP is unchanged:1970-01-01 00:00:00. -
var3:
timezone3isAmerica/New_York(UTC-5). Midnight in New York equals1970-01-01 05:00:00UTC, which the Flink runtime inAsia/Shanghai(UTC+8) displays as1970-01-01 13:00:00.