目录
时间/日期操作符
日期/时间函数
EXTRACT函数
综合示例
时间/日期操作符
操作符 | 例子 | 结果 |
+ | date '2011-09-28' + integer '7' | date '2011-10-05' |
+ | date '2011-09-18' + interval '1 hour' | timestamp '2011-09-18 01:00' |
+ | date '2011-09-18' + time '02:00' | timestamp '2011-09-18 02:00' |
+ | interval '1 day' + interval '1 hour' | interval '1 day 01:00' |
+ | timestamp '2011-09-28 01:00' + interval '23 hours' | timestamp '2011-09-29 00:00' |
+ | time '01:00' + interval '3 hours' | time '04:00' |
- | - interval '23 hours' | interval '-23:00' |
- | date '2001-10-01' - date '2001-09-28' | integer '3' |
- | date '2011-10-01' - integer '7' | date '2011-09-24' |
- | date '2011-09-28' - interval '1 hour' | timestamp '2011-09-27 23:00' |
- | time '05:00' - time '03:00' | interval '02:00' |
- | time '05:00' - interval '2 hours' | time '03:00' |
- | timestamp '2011-09-28 23:00' - interval '23 hours' | timestamp '2011-09-28 00:00' |
- | interval '1 day' - interval '1 hour' | interval '23:00' |
- | timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00' | interval '1 day 15:00' |
* | interval '1 hour' * double precision '3.5' | interval '03:30' |
/ | interval '1 hour' / double precision '1.5' | interval '00:40' |
日期/时间函数
函数 | 返回类型 | 描述 | 例子 | 结果 |
age(timestamp, timestamp) | interval | 减去参数,生成一个使用年、月的"符号化"的结果 | age('2001-04-10', timestamp '1957-06-13') | 43 years 9 mons 27 days |
age(timestamp) | interval | 从current_date减去得到的数值 | age(timestamp '1957-06-13') | 43 years 8 mons 3 days |
current_date | date | 今天的日期 | ||
current_time | time | 现在的时间 | ||
current_timestamp | timestamp | 日期和时间 | ||
date_part(text, timestamp) | double | 获取子域(等效于extract) | date_part('hour', timestamp '2012-05-06 10:20:30') | 10 |
date_part(text, interval) | double | 获取子域(等效于extract) | date_part('month', interval '2 years 3 months') | 3 |
date_trunc(text, timestamp) | timestamp | 截断成指定的精度 | date_trunc('hour', timestamp '2012-05-06 10:20:30') | 2012-05-06 10:00:00+00 |
extract(field from timestamp) | double | 获取子域 | extract(hour from timestamp '2012-05-06 10:20:30') | 10 |
extract(field from interval) | double | 获取子域 | extract(month from interval '2 years 3 months') | 3 |
localtime | time | 今日的时间 | ||
localtimestamp | timestamp | 日期和时间 | ||
now() | timestamp | 当前的日期和时间(等效于 current_timestamp) | ||
timeofday() | text | 当前日期和时间 |
EXTRACT函数
用于取出时间中的一部分,比如取出小时部分,参数示例如下:
域 | 描述 | 例子 | 结果 |
CENTURY | 世纪 | EXTRACT(CENTURY FROM TIMESTAMP '2000-12-16 12:21:13'); | 20 |
DAY | (月分)里的日期域(1-31) | EXTRACT(DAY from TIMESTAMP '2001-02-16 20:38:40'); | 16 |
DECADE | 年份域除以10 | EXTRACT(DECADE from TIMESTAMP '2001-02-16 20:38:40'); | 200 |
DOW | 每周的星期号(0-6;星期天是0) (仅用于timestamp) | EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40'); | 5 |
DOY | 一年的第几天(1 -365/366) (仅用于 timestamp) | EXTRACT(DOY from TIMESTAMP '2001-02-16 20:38:40'); | 47 |
HOUR | 小时域(0-23) | EXTRACT(HOUR from TIMESTAMP '2001-02-16 20:38:40'); | 20 |
MICROSECONDS | 秒域,包括小数部分,乘以 1,000,000。 | EXTRACT(MICROSECONDS from TIME '17:12:28.5'); | 28500000 |
MILLENNIUM | 千年 | EXTRACT(MILLENNIUM from TIMESTAMP '2001-02-16 20:38:40'); | 3 |
MILLISECONDS | 秒域,包括小数部分,乘以 1000。 | EXTRACT(MILLISECONDS from TIME '17:12:28.5'); | 28500 |
MINUTE | 分钟域(0-59) | EXTRACT(MINUTE from TIMESTAMP '2001-02-16 20:38:40'); | 38 |
MONTH | 对于timestamp数值,它是一年里的月份数(1-12);对于interval数值,它是月的数目,然后对12取模(0-11) | EXTRACT(MONTH from TIMESTAMP '2001-02-16 20:38:40'); | 2 |
QUARTER | 该天所在的该年的季度(1-4)(仅用于 timestamp) | EXTRACT(QUARTER from TIMESTAMP '2001-02-16 20:38:40'); | 1 |
SECOND | 秒域,包括小数部分(0-59[1]) | EXTRACT(SECOND from TIMESTAMP '2001-02-16 20:38:40'); | 40 |
WEEK | 该天在所在的年份里是第几周。 | EXTRACT(WEEK from TIMESTAMP '2001-02-16 20:38:40'); | 7 |
YEAR | 年份域 | EXTRACT(YEAR from TIMESTAMP '2001-02-16 20:38:40'); | 2001 |
综合示例
获取当前据本周五23:59还有多少小时多少分钟
select EXTRACT(HOUR FROM(CURRENT_DATE + time '23:59') - CURRENT_TIMESTAMP) + (6 - extract(dow from current_date) )*24 "hour",
EXTRACT(MINUTE FROM(CURRENT_DATE + time '23:59') - CURRENT_TIMESTAMP) "minute";