Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.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 当列值为null时,不要在where条件中附加“and”子句_Sql_Oracle_Spring Data Jpa - Fatal编程技术网

Sql 当列值为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

我有一个查询,当列值为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规范。

您可以使用或:


你可以看看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);