Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 查找给定年份和月份之间的日期范围_Sql_Postgresql_Datetime_Where Clause_Postgresql 12 - Fatal编程技术网

Sql 查找给定年份和月份之间的日期范围

Sql 查找给定年份和月份之间的日期范围,sql,postgresql,datetime,where-clause,postgresql-12,Sql,Postgresql,Datetime,Where Clause,Postgresql 12,我的日期范围如下表所示: create table stf ( d1 date, d2 date ); insert into stf values ('2020-01-01','2020-12-31'), ('2020-11-25','2020-11-25'), ('2019-01-01','2020-11-29'), ('2020-11-01','2021-01-06'), ('2019-01-23','2021-06-06'), ('2019-01-20','2019-1

我的日期范围如下表所示:

create table stf
(
    d1 date,
    d2 date
);

insert into stf values
('2020-01-01','2020-12-31'),
('2020-11-25','2020-11-25'),
('2019-01-01','2020-11-29'),
('2020-11-01','2021-01-06'),
('2019-01-23','2021-06-06'),
('2019-01-20','2019-12-30');
我正在寻找的日期范围是当前日期的月份和年份之间

预期结果应为:对于当前日期

d1      d2
--------------------------
2020-01-01  2020-12-31
2019-01-01  2020-11-29
2020-11-01  2021-01-06
2019-01-23  2021-06-06
2020-11-25  2020-11-25
注意:
2020-11
将介于上述范围之间

尝试1

select * 
from stf
where to_char(now(),'YYYY-MM') between to_char(d1,'YYYY-MM') and to_char(d2,'YYYY-MM');
select * 
from stf
where ((date_part('month',now()) between date_part('month',d1) and date_part('month',d2))or date_part('year',now()) between date_part('year',d1) and date_part('year',d2))
输出:

d1      d2
--------------------------
2020-01-01  2020-12-31
2020-11-25  2020-11-25
2019-01-01  2020-11-29
2020-11-01  2021-01-06
2019-01-23  2021-06-06
d1      d2
--------------------------
2020-01-01  2020-12-31
2020-11-25  2020-11-25
2019-01-01  2020-11-29
2020-11-01  2021-01-06
2019-01-23  2021-06-06
试试2

select * 
from stf
where to_char(now(),'YYYY-MM') between to_char(d1,'YYYY-MM') and to_char(d2,'YYYY-MM');
select * 
from stf
where ((date_part('month',now()) between date_part('month',d1) and date_part('month',d2))or date_part('year',now()) between date_part('year',d1) and date_part('year',d2))
输出:

d1      d2
--------------------------
2020-01-01  2020-12-31
2020-11-25  2020-11-25
2019-01-01  2020-11-29
2020-11-01  2021-01-06
2019-01-23  2021-06-06
d1      d2
--------------------------
2020-01-01  2020-12-31
2020-11-25  2020-11-25
2019-01-01  2020-11-29
2020-11-01  2021-01-06
2019-01-23  2021-06-06

您只需检查当前月初是否属于以下范围:

select *
from stf
where date_trunc('month', current_date) between d1 and d2

缺少此范围
2020-11-25 2020-11-25
。对不起,我的不好,没有在预期结果中提到。现在编辑。我想这应该可以
to_char(now(),'YYYYMM')介于to_char(d1,'YYYYMM')和to_char(d2,'yyyyymm')之间。
对吗?@MAK:是的。或者是介于日期(月份,d1)和日期(月份,d2)之间的日期(月份,当前日期)