Sql 当列值为null时,不要在where条件中附加“and”子句
我有一个查询,当列值为null时,我不想在where条件中附加and子句,例如Sql 当列值为null时,不要在where条件中附加“and”子句,sql,oracle,spring-data-jpa,Sql,Oracle,Spring Data Jpa,我有一个查询,当列值为null时,我不想在where条件中附加and子句,例如 select * from x where x.abc = 'ACTIVE' and x.start_date < sysdate and x.end_date > sysdate 所以在这里,我希望x.end_date仅在其值不为null时才适用。没有任何存储过程,只是一个简单的查询 我还想使用criteria builder和谓词将此查询转换为spring数据jpa规范。您可以使用或: 你可以看看N
select * from x where x.abc = 'ACTIVE' and x.start_date < sysdate and x.end_date > sysdate
所以在这里,我希望x.end_date仅在其值不为null时才适用。没有任何存储过程,只是一个简单的查询
我还想使用criteria builder和谓词将此查询转换为spring数据jpa规范。您可以使用或:
你可以看看NVL函数
select * from x
where x.abc = 'ACTIVE'
and x.start_date < sysdate
and NVL(x.end_date,sysdate) >= sysdate
并不是说其他答案不正确或不好,但我会写得有点不同,我把它贴在这里,希望有人对为什么一种方法可能比另一种更好有一个有趣的评论 下面是我要写的:
select * from x
where x.abc = 'ACTIVE'
and sysdate between x.start_date and nvl(x.end_date,sysdate);
对我来说,这意味着:当前日期必须在记录的活动日期范围内
select * from x
where x.abc = 'ACTIVE'
and sysdate between x.start_date and nvl(x.end_date,sysdate);