select now();
select current_timestamp;
select to_char( now(),'yyyy-mm-dd hh:mi:ss');
注意:如果是表的话,从时间戳字段中提取小时(hh24):
select to_char(时间戳字段,'yyyy-mm-dd hh24:mi:ss');
hh默认是12,可指定:hh12,hh24。
select current_date;
select to_char( now(),'YYYY-MM-DD');
select current_time;
select to_char( now() - interval '1 day','yyyy-mm-dd');
select to_char( now() - interval '1 week','yyyy-mm-dd hh:mi:ss');
select to_char( now() - interval '1 month','yyyy-mm-dd');
select to_char( now() - interval '1 year','yyyy-mm-dd');
select to_char( now() - interval '2 year','yyyy');
select date_trunc('year', now())
select date_trunc('month', now())
select date_trunc('day', now())
select date_trunc('hour', now())
select date_trunc('minute', now())
select date_trunc('second', now())
select * from testdb where timestamp_start >= date_trunc( 'day', now() );
select * from testdb where timestamp_start >= date_trunc( 'month', now() );
select * from testdb where timestamp_start >= date_trunc( 'year', now() );
select * from testdb where timestamp_start >= current_timestamp - interval ' 1 seconds '
select * from testdb where timestamp_start >= current_timestamp - interval ' 1 minutes'
select * from testdb where timestamp_start >= current_timestamp - interval ' 1 hours'
select * from testdb where timestamp_start >= current_timestamp - interval ' 1 day'
select * from testdb where timestamp_start >= current_timestamp - interval ' 7 day'
select * from testdb where timestamp_start >= current_timestamp - interval ' 1 month'
select * from testdb where timestamp_start >= current_timestamp - interval ' 1 year'
select date_part('year', timestamp '2001-02-16 20:38:40')
select date_part('month', timestamp '2001-02-16 20:38:40')
select date_part('day', timestamp '2001-02-16 20:38:40')
select date_part('hour', timestamp '2001-02-16 20:38:40')
select date_part('minute', timestamp '2001-02-16 20:38:40')
select date_part('second', timestamp '2001-02-16 20:38:40')
select date_part('week', timestamp '2001-02-16 20:38:40')
注意:假设当前(now)时间戳是2022-02-28,从时间戳中获取年份可有如下方式:
select date_part('year', current_timestamp)
select date_part('year', now())
select date_part('year', timestamp '2022-02-28')
select date_part('year','2022-02-28'::timestamp)
select date_part('year', 数据表中的时间戳字段名)
注意:可提取year、month、day、hour、minute、second
注意:提取时间戳中的各个年月日时分秒有以下两种方式:
- select date_part('hour', 数据表中的时间戳字段名) 提取的小时如:1,2,3,,23,24
- select to_char(数据表中的时间戳字段名,'hh24'); 提取的小时如:01,02,03,,23,24
SELECT CURRENT_TIMESTAMP,EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP- TIMESTAMP '2023-02-06 15:15:00')) /60 as interval_minutes;