parse_to_timestamp function

The parse_to_timestamp function converts a string into a timestamp, requiring the pattern parameter to match the format of the specified string.

Syntax:

TIMESTAMP parse_to_timestamp(<string>[, pattern])

Semantics:

Note: The string argument with the timestamp uses TimeZoneID (an identifier that represents a specific timezone). Except for UTC and GMT, use the well-defined names for the timezones instead of abbreviations (for example, PST, IST).

Example 1 - Parse the streaming account’s expiry date string for a user using a specified pattern.

SELECT parse_to_timestamp(sa.account_expiry,"yyyy-MM-dd'T'hh:mm:ss.SSSSSSSSSzzzz")AS DAY
FROM stream_acct sa
WHERE acct_id=1

Explanation: In the query, you must supply a pattern that matches the format of the string argument, and the parse_to_timestamp function will convert the account_expiry date into a timestamp value.

Output:

{"DAY":"2023-10-18T00:00:00.000000000Z"}

Example 2 - Parse the given string with the specified pattern, which includes a zone symbol, into a timestamp.

SELECT parse_to_timestamp('03/14/23 09:04:01 America/Los_Angeles', "MM/dd/yy HH:mm:ss VV") AS TIMESTAMP
FROM BaggageInfo
WHERE ticketNo=1762390789239

Explanation: In this query, use the zone symbol, ‘VV’, for any TimeZoneID in the string argument. Unless wrapped in a format_timestamp function, the output timestamp will be displayed relative to UTC.

Output:

{"TIMESTAMP":"2023-03-14T16:04:01.000000000Z"}

Example 3 - Parse the given string with the specified pattern, which includes a zone offset, into a timestamp.

SELECT format_timestamp(parse_to_timestamp('2024/02/12 18:30:54 GMT+02:00', "yyyy/dd/MM HH:mm:ss OOOO"),"yyyy-MM-dd HH:mm:ss OOOO","GMT+02:00")AS TIMESTAMP
FROM BaggageInfo
WHERE ticketNo=1762390789239

Explanation: In this query, the string argument has a TimeZoneID, GMT+02:00, so the pattern argument must include a zone symbol or a ZoneOffset. When wrapped in the format_timestamp function, the output timestamp will display in the GMT+02:00 timezone.

Output:

{"TIMESTAMP":"2024-12-02 18:30:54 GMT+02:00"}