日期和时间函数和运算符¶
日期和时间运算符¶
运算符 |
示例 |
结果 |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
时区转换¶
AT TIME ZONE 运算符设置时间戳的时区
SELECT timestamp '2012-10-31 01:00 UTC';
2012-10-31 01:00:00.000 UTC
SELECT timestamp '2012-10-31 01:00 UTC' AT TIME ZONE 'America/Los_Angeles';
2012-10-30 18:00:00.000 America/Los_Angeles
日期和时间函数¶
- current_date -> date()¶
返回查询开始时的当前日期。
- current_time -> time with time zone()¶
返回查询开始时的当前时间。
- current_timestamp -> timestamp with time zone()¶
返回查询开始时的当前时间戳。
- current_timezone() -> varchar()¶
以 IANA 定义的格式返回当前时区(例如,
America/Los_Angeles)或作为相对于 UTC 的固定偏移量(例如,+08:35)
- date(x) -> date()¶
这是
CAST(x AS date)的别名。
- last_day_of_month(x) -> date()¶
返回该月的最后一天。
- from_iso8601_timestamp(string) -> timestamp with time zone()¶
将 ISO 8601 格式的
string解析为timestamp with time zone。
- from_iso8601_date(string) -> date()¶
将 ISO 8601 格式的
string解析为date。
- from_unixtime(unixtime) -> timestamp()¶
将 UNIX 时间戳
unixtime作为时间戳返回。
- from_unixtime(unixtime, string) -> timestamp with time zone()¶
使用
string作为时区,将 UNIX 时间戳unixtime作为带有时区的时间戳返回。
- from_unixtime(unixtime, hours, minutes) -> timestamp with time zone()¶
使用
hours和minutes作为时区偏移量,将 UNIX 时间戳unixtime作为带有时区的时间戳返回。
- localtime -> time()¶
返回查询开始时的当前时间。
- localtimestamp -> timestamp()¶
返回查询开始时的当前时间戳。
- now() -> timestamp with time zone()¶
这是
current_timestamp的别名。
- to_iso8601(x) -> varchar()¶
将
x格式化为 ISO 8601 字符串。x可以是日期、时间戳或带有时区的时间戳。
- to_milliseconds(interval) -> bigint()¶
将天到秒的
interval作为毫秒返回。
- to_unixtime(timestamp) -> double()¶
将
timestamp作为 UNIX 时间戳返回。
注意
以下 SQL 标准函数不使用括号
current_datecurrent_timecurrent_timestamplocaltimelocaltimestamp
截断函数¶
The date_trunc 函数支持以下单位
单位 |
示例截断值 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
以上示例使用时间戳 2001-08-22 03:04:05.321 作为输入。
- date_trunc(unit, x) -> [same as input]()¶
返回
x截断到unit。
间隔函数¶
本节中的函数支持以下间隔单位
单位 |
描述 |
|---|---|
|
毫秒 |
|
秒 |
|
分钟 |
|
小时 |
|
天 |
|
周 |
|
月 |
|
年的一季度 |
|
年 |
- date_add(unit, value, timestamp) -> [same as input]()¶
将类型为
unit的间隔value添加到timestamp。减法可以通过使用负值来执行。
- date_diff(unit, timestamp1, timestamp2) -> bigint()¶
返回
timestamp2 - timestamp1,以unit表示。
持续时间函数¶
The parse_duration 函数支持以下单位
单位 |
描述 |
|---|---|
|
纳秒 |
|
微秒 |
|
毫秒 |
|
秒 |
|
分钟 |
|
小时 |
|
天 |
- parse_duration(string) -> interval()¶
将格式为
value unit的string解析为间隔,其中value是unit值的小数部分SELECT parse_duration('42.8ms'); -- 0 00:00:00.043 SELECT parse_duration('3.81 d'); -- 3 19:26:24.000 SELECT parse_duration('5m'); -- 0 00:05:00.000
MySQL 日期函数¶
本节中的函数使用与 MySQL date_parse 和 str_to_date 函数兼容的格式字符串。以下表格根据 MySQL 手册,描述了格式说明符
说明符 |
描述 |
|---|---|
|
缩写的星期几名称 ( |
|
缩写的月份名称 ( |
|
月份,数字 ( |
|
带英文后缀的月份天数 ( |
|
月份天数,数字 ( |
|
月份天数,数字 ( |
|
秒的小数部分 (打印 6 位数: |
|
小时 ( |
|
小时 ( |
|
小时 ( |
|
分钟,数字 ( |
|
年中的天数 ( |
|
小时 ( |
|
小时 ( |
|
月份名称 ( |
|
月份,数字 ( |
|
|
|
时间,12 小时制 ( |
|
秒 ( |
|
秒 ( |
|
时间,24 小时制 ( |
|
周 ( |
|
周 ( |
|
周 ( |
|
周 ( |
|
星期几名称 ( |
|
周中的天数 ( |
|
以星期日为周的第一天,该周的年份,数字,四位数;与 |
|
周中年份,周一为一周的第一天,数字,四位数;与 |
|
年份,数字,四位数 |
|
年份,数字(两位数) [2] |
|
一个字面 |
|
|
警告
以下说明符目前尚不支持:%D %U %u %V %w %X
- date_format(timestamp, format) -> varchar()¶
使用
format将timestamp格式化为字符串。
- date_parse(string, format) -> timestamp()¶
使用
format将string解析为时间戳。
Java 日期函数¶
本节中的函数使用与 JodaTime 的 DateTimeFormat 模式格式兼容的格式字符串。
- format_datetime(timestamp, format) -> varchar()¶
使用
format将timestamp格式化为字符串。
- parse_datetime(string, format) -> timestamp with time zone()¶
使用
format将string解析为带时区的时间戳。
提取函数¶
extract 函数支持以下字段
字段 |
描述 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extract 函数支持的类型取决于要提取的字段。大多数字段支持所有日期和时间类型。
- extract(field FROM x) -> bigint()¶
从
x返回field。注意
此 SQL 标准函数使用特殊语法来指定参数。
便捷提取函数¶
- day(x) -> bigint()¶
从
x返回月份中的日期。
- day_of_month(x) -> bigint()¶
这是
day()的别名。
- day_of_week(x) -> bigint()¶
从
x返回 ISO 星期几。该值范围从1(星期一)到7(星期日)。
- day_of_year(x) -> bigint()¶
从
x返回一年中的日期。该值范围从1到366。
- dow(x) -> bigint()¶
这是
day_of_week()的别名。
- doy(x) -> bigint()¶
这是
day_of_year()的别名。
- hour(x) -> bigint()¶
从
x返回一天中的小时数。该值范围从0到23。
- millisecond(x) -> bigint()¶
从
x返回秒中的毫秒数。
- minute(x) -> bigint()¶
从
x返回小时中的分钟数。
- month(x) -> bigint()¶
从
x返回一年中的月份。
- quarter(x) -> bigint()¶
从
x返回一年中的季度。该值范围从1到4。
- second(x) -> bigint()¶
从
x返回分钟中的秒数。
- timezone_hour(timestamp) -> bigint()¶
从
timestamp返回时区偏移量的小时数。
- timezone_minute(timestamp) -> bigint()¶
从
timestamp返回时区偏移量的分钟数。
- week_of_year(x) -> bigint()¶
这是
week()的别名。
- year(x) -> bigint()¶
从
x返回年份。
- yow(x) -> bigint()¶
这是
year_of_week()的别名。