- Categories:
TIMESTAMP_FROM_PARTS¶
Creates a timestamp from individual numeric components. If no time zone is in effect, the function can be used to create a timestamp from a date expression and a time expression.
- Aliases:
TIMESTAMPFROMPARTS
- Variations (and Aliases):
TIMESTAMP_LTZ_FROM_PARTS , TIMESTAMPLTZFROMPARTS
TIMESTAMP_NTZ_FROM_PARTS , TIMESTAMPNTZFROMPARTS
TIMESTAMP_TZ_FROM_PARTS , TIMESTAMPTZFROMPARTS
Syntax¶
Note
The date and time expression version of TIMESTAMP_FROM_PARTS is only valid when the TIMESTAMP_TYPE_MAPPING session parameter is set to TIMESTAMP_NTZ.
Arguments¶
Required:
yearAn integer expression to use as a year for building a timestamp.
monthAn integer expression to use as a month for building a timestamp, with January represented as
1, and December as12.dayAn integer expression to use as a day for building a timestamp, usually in the
1-31range.hourAn integer expression to use as an hour for building a timestamp, usually in the
0-23range.minuteAn integer expression to use as a minute for building a timestamp, usually in the
0-59range.secondAn integer expression to use as a second for building a timestamp, usually in the
0-59range.date_expr,time_exprSpecifies the date and time expressions to use for building a timestamp where
date_exprprovides the year, month, and day for the timestamp andtime_exprprovides the hour, minute, second, and nanoseconds within the day. Only valid for:TIMESTAMP_FROM_PARTS (when the TIMESTAMP_TYPE_MAPPING session parameter is set to TIMESTAMP_NTZ)
TIMESTAMP_NTZ_FROM_PARTS
Optional:
nanosecondsAn integer expression to use as a nanosecond for building a timestamp, usually in the
0-999999999range.time_zoneA string expression to use as a time zone for building a timestamp (e.g.
America/Los_Angeles). Only valid for:TIMESTAMP_FROM_PARTS (when the TIMESTAMP_TYPE_MAPPING session parameter is set to TIMESTAMP_TZ)
TIMESTAMP_TZ_FROM_PARTS
Usage notes¶
TIMESTAMP_FROM_PARTS variations are typically used to handle values in the “normal” value ranges (e.g. months
1-12, days1-31, hours0-23, etc.); however, they can also handle values from outside these ranges. This allows choosing the Nth day in a year or Nth second in a day, which can be useful for simplifying some computations.TIMESTAMP_FROM_PARTS is equivalent to the variation specified by the TIMESTAMP_TYPE_MAPPING session parameter (default is TIMESTAMP_NTZ).
Examples¶
Set the session variables that control output format and time zone:
Using TIMESTAMP_LTZ_FROM_PARTS:
Using TIMESTAMP_NTZ_FROM_PARTS:
Using TIMESTAMP_NTZ_FROM_PARTS with a date and time rather than with
year, month, day, hour, etc.:
Using TIMESTAMP_TZ_FROM_PARTS with a session-default time zone (‘America/New_York’/-0400):
Using TIMESTAMP_TZ_FROM_PARTS with a specified time zone (‘America/Los_Angeles’/-0700); note also the use of 0 as the nanoseconds argument:
Handling values outside normal ranges (subtracting 1 hour by specifying -3600 seconds):