- 类别:
DATE_PART¶
从日期、时间或时间戳中提取指定的日期或时间部分。
语法¶
DATE_PART( <date_or_time_part> , <date_or_timetamp_expr> )
DATE_PART( <date_or_time_part> FROM <date_or_time_expr> )
实参¶
date_or_time_part
时间单位。必须是 支持的日期和时间部分 中列出的一个值(例如
month
)。该值可以是字符串字面量,也可以不带引号(例如'month'
或month
)。当
date_or_time_part
为week`(或其任何变体)时,输出由 :ref:`label-week_start
会话参数控制。当
date_or_time_part
为dayofweek
或yearofweek`(或其任何变体)时,输出由 :ref:`label-week_of_year_policy
和 WEEK_START 会话参数控制。
有关更多详细信息(包括示例),请参阅 日历周和工作日。
date_or_timestamp_expr
日期、时间、时间戳或计算结果为日期、时间或时间戳的表达式。
返回¶
返回 NUMBER 数据类型的值。
示例¶
这显示了提取 DATE 部分内容的简单示例:
SELECT DATE_PART(quarter, '2024-04-08'::DATE);
+----------------------------------------+
| DATE_PART(QUARTER, '2024-04-08'::DATE) |
|----------------------------------------|
| 2 |
+----------------------------------------+
这显示了提取 TIMESTAMP 部分内容的示例:
SELECT TO_TIMESTAMP(
'2024-04-08T23:39:20.123-07:00') AS "TIME_STAMP1",
DATE_PART(year, "TIME_STAMP1") AS "EXTRACTED YEAR";
+-------------------------+----------------+
| TIME_STAMP1 | EXTRACTED YEAR |
|-------------------------+----------------|
| 2024-04-08 23:39:20.123 | 2024 |
+-------------------------+----------------+
这显示了将 TIMESTAMP 转换为自 Unix 纪元 (link removed) 开始(1970 年 1 月 1 日午夜)以来秒数的示例:
SELECT TO_TIMESTAMP(
'2024-04-08T23:39:20.123-07:00') AS "TIME_STAMP1",
DATE_PART(epoch_second, "TIME_STAMP1") AS "EXTRACTED EPOCH SECOND";
+-------------------------+------------------------+
| TIME_STAMP1 | EXTRACTED EPOCH SECOND |
|-------------------------+------------------------|
| 2024-04-08 23:39:20.123 | 1712619560 |
+-------------------------+------------------------+
这显示了将 TIMESTAMP 转换为自 Unix 纪元 (link removed) 开始(1970 年 1 月 1 日午夜)以来毫秒数的示例:
SELECT TO_TIMESTAMP(
'2024-04-08T23:39:20.123-07:00') AS "TIME_STAMP1",
DATE_PART(epoch_millisecond, "TIME_STAMP1") AS "EXTRACTED EPOCH MILLISECOND";
+-------------------------+-----------------------------+
| TIME_STAMP1 | EXTRACTED EPOCH MILLISECOND |
|-------------------------+-----------------------------|
| 2024-04-08 23:39:20.123 | 1712619560123 |
+-------------------------+-----------------------------+