码迷,mamicode.com
首页 > 数据库 > 详细

PostgreSQL时间段查询

时间:2018-11-14 14:15:41      阅读:1917      评论:0      收藏:0      [点我收藏+]

标签:extract   当前时间   highlight   res   inter   pre   to_date   说明   text   

1.今日

select *  from "表名" 
where to_date("时间字段"::text,‘yyyy-mm-dd‘)=current_date

2.昨日

select *  from "表名" 
where to_date("时间字段"::text,‘yyyy-mm-dd‘)=current_date - 1

3.最近半个月

select *  from "表名" 
where to_date("时间字段"::text,‘yyyy-mm-dd‘) BETWEEN current_date - interval ‘15 day‘  AND current_date

4.最近6个月

select *  from "表名" 
where to_date("时间字段"::text,‘yyyy-mm-dd‘) BETWEEN current_date - (‘6month ‘ || extract(day from CURRENT_DATE) -1 || ‘ day‘)::interval  AND current_date

说明:

extract(day from CURRENT_DATE) 提取当前时间的天数,因为查询最近六个月,比如现在2018年11月14日,查询的时间区间是
2018年5月1日 - 2018年11月14日 

当前时间减去6个月和13天,得到2018年5月1日(如果减去14天得到的是2018年4月30日)
select current_date - (‘6 month ‘ || extract(day from CURRENT_DATE) -1 || ‘ day‘)::interval as beginMonth ;

 查询结果:2018-05-01 00:00:00

 

PostgreSQL时间段查询

标签:extract   当前时间   highlight   res   inter   pre   to_date   说明   text   

原文地址:https://www.cnblogs.com/liuxiutianxia/p/9957391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!