添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

source TIMESTAMP INTERVAL 类型的值。如果传递一个 DATE 值,该函数会将其转换为一个 TIMESTAMP 值。

EXTRACT() 函数返回一个双精度浮点数值。

A) 从时间戳中提取的示例

从时间戳中提取年份:

SELECT EXTRACT(YEAR FROM TIMESTAMP '2016-12-31 13:30:15');

结果如下:

从时间戳中提取季度:

SELECT EXTRACT(QUARTER FROM TIMESTAMP '2016-12-31 13:30:15');

从时间戳中提取月份:

SELECT EXTRACT(MONTH FROM TIMESTAMP '2016-12-31 13:30:15');

结果如下:

从时间戳中提取日:

SELECT EXTRACT(DAY FROM TIMESTAMP '2016-12-31 13:30:15');

这是结果:

从时间戳中提取世纪:

SELECT EXTRACT(CENTURY FROM TIMESTAMP '2016-12-31 13:30:15');

正如预期的那样,它返回了 21:

从时间戳中提取十年:

SELECT EXTRACT(DECADE FROM TIMESTAMP '2016-12-31 13:30:15');

结果如下:

从时间戳中提取星期几:

SELECT EXTRACT(DOW FROM TIMESTAMP '2016-12-31 13:30:15');

从时间戳中提取一年中的第几天:

SELECT EXTRACT(DOY FROM TIMESTAMP '2016-12-31 13:30:15');

它返回 366:

从时间戳中提取自 1970-01-01 00:00:00 UTC 以来的秒数:

SELECT EXTRACT(EPOCH FROM TIMESTAMP '2016-12-31 13:30:15');
1483191015

从时间戳中提取小时部分:

SELECT EXTRACT(HOUR FROM TIMESTAMP '2016-12-31 13:30:15');

从时间戳中提取分钟部分:

SELECT EXTRACT(MINUTE FROM TIMESTAMP '2016-12-31 13:30:15');

结果如下:

从时间戳中提取秒部分:

SELECT EXTRACT(SECOND FROM TIMESTAMP '2016-12-31 13:30:15.45');

结果包括秒及其小数部分:

15.45

根据 ISO 8601 提取工作日:

SELECT EXTRACT(ISODOW FROM TIMESTAMP '2016-12-31 13:30:15');

从时间戳中提取毫秒:

SELECT EXTRACT(MILLISECONDS FROM TIMESTAMP '2016-12-31 13:30:15');

结果是 15 * 1000 = 15000

15000

从时间戳中提取微秒:

SELECT EXTRACT(MICROSECONDS FROM TIMESTAMP '2016-12-31 13:30:15');

结果是 15 * 1000000 = 15000000

15000000

B) 从间隔值中提取的示例

从间隔值中提取年份:

SELECT EXTRACT(YEAR FROM INTERVAL '6 years 5 months 4 days 3 hours 2 minutes 1 second' );

从间隔值中提取季度:

SELECT EXTRACT(QUARTER FROM INTERVAL '6 years 5 months 4 days 3 hours 2 minutes 1 second' );

从间隔值中提取月份:

SELECT EXTRACT(MONTH FROM INTERVAL '6 years 5 months 4 days 3 hours 2 minutes 1 second' );

从间隔值中提取日:

SELECT EXTRACT(DAY FROM INTERVAL '6 years 5 months 4 days 3 hours 2 minutes 1 second' );

从间隔值中提取小时部分:

SELECT EXTRACT(HOUR FROM INTERVAL '6 years 5 months 4 days 3 hours 2 minutes 1 second' );

从间隔值中提取分钟部分:

SELECT EXTRACT(MINUTE FROM INTERVAL '6 years 5 months 4 days 3 hours 2 minutes 1 second' );

从间隔值中提取秒数部分:

SELECT EXTRACT(SECOND FROM INTERVAL '6 years 5 months 4 days 3 hours 2 minutes 1 second' );

从间隔值中提取毫秒:

SELECT EXTRACT(MILLISECONDS FROM INTERVAL '6 years 5 months 4 days 3 hours 2 minutes 1 second' );

从间隔值中提取微秒:

SELECT EXTRACT(MICROSECONDS FROM INTERVAL '6 years 5 months 4 days 3 hours 2 minutes 1 second' );
1000000

从间隔值中提取十年:

SELECT EXTRACT(DECADE FROM INTERVAL '60 years 5 months 4 days 3 hours 2 minutes 1 second' );

从间隔值中提取千年:

SELECT EXTRACT(MILLENNIUM FROM INTERVAL '1999 years 5 months 4 days 3 hours 2 minutes 1 second' );

从间隔值中提取世纪:

SELECT EXTRACT(CENTURY FROM INTERVAL '1999 years 5 months 4 days 3 hours 2 minutes 1 second' ); 

在本教程中,您学习了如何从日期/时间或间隔值中提取字段。

PostgreSQL 教程:日期函数

PostgreSQL 文档:时间/日期函数和操作符