sql 查询中未检索实际数据的日期比较
Comparison of dates not retrieving actual data in sql query
我正在尝试过滤掉日期“31-01-2020”之前和“01-01-2020”之后的数据,但以下查询不起作用。我是否遗漏了日期比较的内容?
select *
from per_all_assignments_m paam
where 1 = 1
and TO_CHAR(paam.effective_start_date, 'DD-MM-YYYY') <= '31-01-2020'
and TO_CHAR(paam.effective_END_date, 'DD-MM-YYYY') >= '01-01-2020'
and assignment_number like '%555%'
and assignment_type = 'E'
不要将日期转换为字符串进行比较。首先,您使用的格式使比较错误。即使你使用了正确的日期格式,这仍然是非常低效的。
相反,将它们与文字日期进行比较:
where
effective_start_date >= date '2020-01-01'
and effective_start_date < date '2020-02-01'
你可以试试
select * from per_all_assignments_m paam
where 1=1
and (paam.effective_start_date <= to_date('31-01-2020', 'dd-mm-rrrr') OR paam.effective_END_date >= to_date('01-01-2020', 'dd-mm-rrrr'))
and assignment_number like '%555%'
and assignment_type = 'E'
我正在尝试过滤掉日期“31-01-2020”之前和“01-01-2020”之后的数据,但以下查询不起作用。我是否遗漏了日期比较的内容?
select *
from per_all_assignments_m paam
where 1 = 1
and TO_CHAR(paam.effective_start_date, 'DD-MM-YYYY') <= '31-01-2020'
and TO_CHAR(paam.effective_END_date, 'DD-MM-YYYY') >= '01-01-2020'
and assignment_number like '%555%'
and assignment_type = 'E'
不要将日期转换为字符串进行比较。首先,您使用的格式使比较错误。即使你使用了正确的日期格式,这仍然是非常低效的。
相反,将它们与文字日期进行比较:
where
effective_start_date >= date '2020-01-01'
and effective_start_date < date '2020-02-01'
你可以试试
select * from per_all_assignments_m paam
where 1=1
and (paam.effective_start_date <= to_date('31-01-2020', 'dd-mm-rrrr') OR paam.effective_END_date >= to_date('01-01-2020', 'dd-mm-rrrr'))
and assignment_number like '%555%'
and assignment_type = 'E'