- 类别:
DATE_PART¶
从日期、时间或时间戳中提取指定的日期或时间部分。
语法¶
实参¶
date_or_time_partThe unit of time. Must be one of the values listed in 支持的日期和时间部分 (for example,
month). The value can be a string literal or can be unquoted (for example,'month'ormonth).当
date_or_time_part为week`(或其任何变体)时,输出由 :ref:`label-week_start会话参数控制。当
date_or_time_part为dayofweek或yearofweek`(或其任何变体)时,输出由 :ref:`label-week_of_year_policy和 WEEK_START 会话参数控制。
有关更多信息(包括示例),请参阅 日历周和工作日。
date_interval_time_or_timestamp_exprA date, an interval, a time, or a timestamp, or an expression that can be evaluated to one of those data types.
返回¶
返回 NUMBER 数据类型的值。
使用说明¶
When
date_interval_time_or_timestamp_expris a year-month interval value, the supporteddate_or_time_partvalues areyearandmonth.When
date_interval_time_or_timestamp_expris a day-time interval value, the supporteddate_or_time_partvalues areday,hour,minute,second, andnanosecond.Currently, when
date_interval_time_or_timestamp_expris a DATE value, the followingdate_or_time_partvalues aren't supported:epoch_millisecondepoch_microsecondepoch_nanosecond
支持其他 日期和时间部分 (包括
epoch_second)。
小技巧
要从 TIMESTAMP 值中提取完整的 DATE 或 TIME 值(而非单个部分),可相应地将 TIMESTAMP 值转换为 DATE 或 TIME 值。例如:
示例¶
这显示了提取 DATE 部分内容的简单示例:
这显示了提取 TIMESTAMP 部分内容的示例:
这显示了将 TIMESTAMP 转换为自 Unix 纪元 (link removed) 开始(1970 年 1 月 1 日午夜)以来秒数的示例:
这显示了将 TIMESTAMP 转换为自 Unix 纪元 (link removed) 开始(1970 年 1 月 1 日午夜)以来毫秒数的示例: