当前位置: 代码网 > it编程>数据库>MsSqlserver > postgresql 日期查询最全整理

postgresql 日期查询最全整理

2024年08月07日 MsSqlserver 我要评论
1、获取当前日期select now();select current_timestamp;返回值均是当前年月日、时分秒,且秒保留6位小数,两种方式等价select current_time;返回值:

1、获取当前日期

select now();

在这里插入图片描述

select current_timestamp;

在这里插入图片描述

返回值均是当前年月日、时分秒,且秒保留6位小数,两种方式等价

select current_time;

在这里插入图片描述

返回值:时分秒,秒最高精确到6位

select current_date;

在这里插入图片描述

返回值:年月日

2、查询今天的数据

select * from 表名 where 时间字段 >= current_date and 时间字段 < current_date + 1;

3、查询昨天的数据

select * from 表名 where 时间字段 >= current_date - 1 and 时间字段 < current_date;

4、查询一个月内的数据

select * from 表名 where 时间字段 >= current_date - interval '1 month' and 时间字段 <= current_date;

5、按日, 周, 月, 季度, 年统计数据

select date_trunc('day', 时间字段) as statistictime, 分组字段, count(0) from 表名 group by date_trunc('day', 时间字段), 分组字段

日: day; 周: week; 月: month; 季度: quarter; 年: year

6、 查询昨天、上周、上月、上年的日期

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');

7、查询今天、今月、今年的开始的日期时间

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())

8、查询最近1秒,1分,1小时,1天,1周(7天),1月,1年的记录

select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 seconds '
select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 minutes'
select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 hours'
select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 day'
select * from 表名 where timestamp_start >= current_timestamp - interval ' 7 day'
select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 month'
select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 year'

9、从时间戳中提取 年月日时分秒、周

select date_part('year', timestamp '2024-02-16 12:38:40')
select date_part('month', timestamp '2024-02-16 12:38:40')
select date_part('day', timestamp '2024-02-16 12:38:40')
select date_part('hour', timestamp '2024-02-16 12:38:40')
select date_part('minute', timestamp '2024-02-16 12:38:40')
select date_part('second', timestamp '2024-02-16 12:38:40')
select date_part('week', timestamp '2024-02-16 12:38:40')

到此这篇关于postgresql 您要的日期查询都在这的文章就介绍到这了,更多相关postgresql 日期查询内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com