选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

文档 / 函数百科 / 时间/日期函数和操作符

FUNCTION时间/日期函数和操作符

extract

获取时间戳子字段;参见 Section 9.9.1

Get timestamp subfield; see Section 9.9.1

时间/日期函数和操作符 引入 9.0(基线) 现存至 20 devel 3 条签名 2 次签名变更

签名数
3 条
引入版本
9.0(基线)
状态
现存
签名变更
2 次
本版来源
本站译文

PostgreSQL 18 手册 · 9.9 时间/日期函数和操作符 官方文档

版本轨迹

相对 PostgreSQL 17 无变化。

签名

PostgreSQL 18 里 extract 的 3 条签名,按上游手册顺序排列。参数名用斜体,类型用弱化色, 之后是返回类型。说明取自本站手册译文:标「沿用」的借用了另一版的译文,标「EN」的本站暂无译文、按英文原文显示。

  1. extract ( field from timestamp ) → numeric

    获取时间戳子字段;参见 Section 9.9.1
    • extract(hour from timestamp '2001-02-16 20:38:40')20
  2. extract ( field from interval ) → numeric

    获取时间间隔子字段;参见 Section 9.9.1
    • extract(month from interval '2 years 3 months')3
  3. EXTRACT(field FROM source)

    extract函数从日期/时间值中检索子字段,例如年份或小时。 source必须是timestampdatetimeinterval类型的值表达式。 (时间戳和时间可以带有或不带有时区。) field是一个标识符或字符串,用于选择从源值中提取哪个字段。 并非每种输入数据类型都适用于所有字段;例如,无法从date中提取小于一天的字段,而无法从time中提取一天或更长时间的字段。 extract函数返回numeric类型的值。

演化历史

相邻两个大版本之间的差异,新的在前。版本号链到该版的签名。

  1. PostgreSQL 19 ← 18 签名变更

    extract ( field FROM timestamp ) → numericextract ( field FROM interval ) → numeric extract ( field from timestamp ) → numericextract ( field from interval ) → numeric

  2. PostgreSQL 14 ← 13 签名变更

    extract ( field from timestamp ) → numericextract ( field from interval ) → numeric extract ( field from timestamp ) → double precisionextract ( field from interval ) → double precision

  3. PostgreSQL 13 ← 12 手册重排

    这一跳手册重排了函数表的写法,签名文本整体改写,只记函数的增删,不逐条比较签名。

签名矩阵

每条签名在给出了签名的 18 个版本里的存在情况;方格指向该版的签名。只在正文里提到此函数、没有给出签名的版本不列进来,上面的版本轨迹里仍然有它们。签名文本在 PostgreSQL 13 前后写法不同,跨越那一跳的同一条签名会显示成两行。

存在 不存在

签名 9.09.19.29.39.49.59.61011121314151617181920
extract ( field from timestamp ) → double precision 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在
extract ( field from interval ) → double precision 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在
EXTRACT(field FROM source) 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在 存在
extract ( field from timestamp ) → numeric 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 存在 存在 存在 存在 存在 不存在 不存在
extract ( field from interval ) → numeric 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 存在 存在 存在 存在 存在 不存在 不存在
extract ( field FROM timestamp ) → numeric 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 存在 存在
extract ( field FROM interval ) → numeric 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 不存在 存在 存在

同组函数

函数 签名 主签名 版本变动 最近变更
DATE/TIME FUNCTIONS AND OPERATORS 时间/日期函数和操作符 29 个
age基线 3 age ( timestamp, timestamp ) → interval
减去参数,生成一个使用年和月,而不是只用日的“符号化”的结果 现存
clock_timestamp基线 2 clock_timestamp ( ) → timestamp with time zone
当前日期和时间(在语句执行期间变化);参见Section 9.9.5 现存
current_date基线 1 current_date → date
当前日期;参见 Section 9.9.5 现存
current_time基线 3 current_time → time with time zone
一天中的当前时间;参见 Section 9.9.5 现存
current_timestamp基线 3 current_timestamp → timestamp with time zone
当前日期和时间 (当前事务的开始);参见 Section 9.9.5 现存
date_add 1 date_add ( timestamp with time zone, interval [, text ] ) → timestamp with time zone
将一个 interval 加到 timestamp with time zone 上,并根据第三个参数命名的时区,或在省略该参数时根据当前TimeZone设置,计算时刻和夏令时调整。 现存
date_bin 2 date_bin ( interval, timestamp, timestamp ) → timestamp
将输入按指定的间隔进行分箱(bin),对齐到指定的原点;参见Section 9.9.3 现存
date_part基线 3 date_part ( text, timestamp ) → double precision
获取时间戳字段 (等同于 extract);参见 Section 9.9.1 现存
date_subtract 1 date_subtract ( timestamp with time zone, interval [, text ] ) → timestamp with time zone
从一个 timestamp with time zone 中减去 interval,并根据第三个参数命名的时区,或在省略该参数时根据当前TimeZone设置,计算时刻和夏令时调整。 现存
date_trunc基线 4 date_trunc ( text, timestamp ) → timestamp 183 次
截断到指定的精度;参见 Section 9.9.2 现存
extract基线 3 extract ( field FROM timestamp ) → numeric 192 次
获取时间戳子字段;参见 Section 9.9.1 现存
isfinite基线 3 isfinite ( date ) → boolean
测试有限日期(不是+/-无限) 现存
justify_days基线 1 justify_days ( interval ) → interval
调整间隔,将30天的时间段转换为月份 现存
justify_hours基线 1 justify_hours ( interval ) → interval
调整间隔,将24小时时间段转换为天数 现存
justify_interval基线 1 justify_interval ( interval ) → interval
使用 justify_days 和 justify_hours调整时间间隔; 通过额外的符号调整 现存
localtime基线 3 localtime → time
一天中当前时间;参见 Section 9.9.5 现存
localtimestamp基线 3 localtimestamp → timestamp
当前日期和时间(当前事务的开始);参见 Section 9.9.5 现存
make_date 1 make_date ( year int, month int, day int ) → date
从年、月和日字段创建日期(负数年份表示BC) 现存
make_interval 1 make_interval ( [ years int [, months int [, weeks int [, days int [, hours int [, mins int [, secs double precision ]]]]]]] ) → interval
从年、月、周、日、小时、分钟和秒字段创建时间间隔,每个字段默认为0 现存
make_time 1 make_time ( hour int, min int, sec double precision ) → time
从小时、分钟和秒字段创建时间 现存
make_timestamp 1 make_timestamp ( year int, month int, day int, hour int, min int, sec double precision ) → timestamp
从年、月、日、小时、分钟和秒字段创建时间戳(负数年份表示BC) 现存
make_timestamptz 1 make_timestamptz ( year int, month int, day int, hour int, min int, sec double precision [, timezone text ] ) → timestamp with time zone
从年,月,日,小时,分钟和秒字段结合时区创建时间戳(负数年份表示BC)。 现存
now基线 2 now ( ) → timestamp with time zone
当前日期和时间(当前事务的开始);参见 Section 9.9.5 现存
pg_sleep基线 1 pg_sleep ( double precision )
pg_sleep使当前会话的进程休眠,直到过去给定的秒数。 现存
pg_sleep_for 1 pg_sleep_for ( interval )
pg_sleep使当前会话的进程休眠,直到过去给定的秒数。 现存
pg_sleep_until 1 pg_sleep_until ( timestamp with time zone )
pg_sleep使当前会话的进程休眠,直到过去给定的秒数。 现存
statement_timestamp基线 2 statement_timestamp ( ) → timestamp with time zone
当前日期和时间(当前语句的开始);参见 Section 9.9.5 现存
timeofday基线 2 timeofday ( ) → text
当前的日期和时间 (类似 clock_timestamp, 但是采用 text 字符串);参见 Section 9.9.5 现存
transaction_timestamp基线 2 transaction_timestamp ( ) → timestamp with time zone
当前日期和时间(当前事务的开始);参见 Section 9.9.5 现存