查找给定年份和月份之间的日期范围
Find the date range between the given year and month
我有以下 table 日期范围:
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');
输出:
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 ((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
您可以只检查当月的开始是否属于以下范围:
select *
from stf
where date_trunc('month', current_date) between d1 and d2
我有以下 table 日期范围:
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');
输出:
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 ((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
您可以只检查当月的开始是否属于以下范围:
select *
from stf
where date_trunc('month', current_date) between d1 and d2