pgsql获取时间的各种格式

2021/12/31 19:14:48

本文主要是介绍pgsql获取时间的各种格式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#获取当前在线人数时间定为上下10 分钟
select * from tbl_base_user_record where module_id = 4 and date_part('minute', now()::TIMESTAMP) - date_part('minute', create_time) <= 10 
and to_char(now(), 'yyyy-MM-dd HH') = to_char(create_time, 'yyyy-MM-dd HH')



#获取这一天是周几
SELECT EXTRACT(DOW FROM TIMESTAMP '2021-11-30 20:38:40');

#月份里的天数
SELECT EXTRACT(DAY FROM TIMESTAMP '2021-12-31 20:38:40');
Result:31

#这一天是一年中的第几天
SELECT EXTRACT(DOY FROM TIMESTAMP '2021-12-31 20:38:40');

#获取是哪一个世纪
SELECT EXTRACT(CENTURY FROM TIMESTAMP '1890-12-16 12:21:13');
Result:19


年份从除以10
SELECT EXTRACT(DECADE FROM TIMESTAMP '2021-02-16 20:38:40');
Result:200

#周中天的索引(0-6 ;星期天是 0)
SELECT EXTRACT(DOW FROM TIMESTAMP '2021-12-31 20:38:40');
Result:5

SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2021-12-31 20:38:40.12-08');
结果:982384720.12
 
SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 6 hours');
结果:442800


SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1641011920.12 * INTERVAL '1 second';

#获取时间中的小时
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 12:38:40');
结果:12

#获取时间中的分钟
SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:28:40');
Result:38

#获取中的第几天
SELECT EXTRACT(ISODOW FROM TIMESTAMP '2021-12-31 20:38:40');
结果:5

#获取日期中的年份
SELECT EXTRACT(ISOYEAR FROM DATE '2002-01-01');
结果:2002


SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
结果:28500000



SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
Result:2

SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
Result:3

SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
Result:1

#获取日期中的月份
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-12-16 20:38:40');
Result:12
 
SELECT EXTRACT(MONTH FROM INTERVAL '12 years 1 months');
Result:3
 
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 7 months');
Result:7

#获取日期中的第几季度
SELECT EXTRACT(QUARTER FROM TIMESTAMP '2021-12-31 10:44:53' );
Result:4

#获取时间中的秒
SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:20');
Result:20

SELECT EXTRACT(SECOND FROM TIME '17:12:33.5');
Result:33.5


#获取日期中的天的日期
SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
Result:16
 
SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
Result:4




这篇关于pgsql获取时间的各种格式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程