日期列中的空值

时间:2016-02-27 19:11:09

标签: sql parameters oracle11g

我有一张包含以下数据的表格:

ITEM   LOCATION   RECEIVE_DATE
001     13001     27-JAN-2016
002     14001       null

我使用以下内容以两个参数检索结果:

select * from item_master
where 
(TRUNC (RECEIVE_DATE) between NVL(:STARTDATE, TRUNC(RECEIVE_DATE)) and NVL(:enddate, trunc(RECEIVE_DATE))); 

如果我将两个日期参数都输入为空,我只获取RECEIVE_DATE的记录并排除所有记录为空RECEIVE_DATE

如果我没有为参数输入任何值,我应该如何检索所有行?

1 个答案:

答案 0 :(得分:2)

您可以添加ORIS NULL

select * 
from item_master
where 
  (TRUNC(RECEIVE_DATE) 
  between NVL(:STARTDATE, TRUNC(RECEIVE_DATE)) and NVL(:enddate, trunc(RECEIVE_DATE)))
  OR (:STARTDATE IS NULL AND :enddate IS NULL);