用于检查给定日期的日期验证在该范围内

时间:2015-09-12 22:13:04

标签: sql oracle

我正在尝试编写查询以同时检查2个或更多日期,以检查其中一个是否超出给定范围。

SELECT 
    sysdate 
FROM dual 
GROUP BY sysdate 
HAVING COUNT( CASE WHEN (SELECT sysdate, (sysdate+1) FROM dual) BETWEEN 
SYSDATE - 10 AND SYSDATE+10 THEN 1 ELSE NULL END ) > 0 

我得到了例外

  

ORA-00913:值太多

试用2个日期时。 请指教

2 个答案:

答案 0 :(得分:1)

不完全确定您为什么要这样的查询。但正如错误所述,when接受一个条件,因此您必须使用and分隔查询。此外,不需要else null部分,因为它是默认值。

SELECT sysdate FROM dual 
GROUP BY sysdate 
HAVING COUNT(
           CASE WHEN (select sysdate from dual) BETWEEN SYSDATE - 10 AND SYSDATE+10 
           AND (select sysdate+1 from dual) BETWEEN SYSDATE - 10 AND SYSDATE+10 
           THEN 1 END
             ) > 0

答案 1 :(得分:0)

问题是vkp建议的情况是when()期望一个值与另一个值进行比较,但sysdate,sysdate + 1返回2个无法一起比较的值。因此最好将两个条件分开,但我会在下面的where子句中完成它

        SELECT 
            sysdate 
        FROM dual 
        GROUP BY sysdate 
        HAVING ( select count(*) from dual 
        WHERE 
            (SELECT sysdate FROM dual) BETWEEN 
            SYSDATE - 10 AND SYSDATE+10 
        AND
            (SELECT sysdate+1 FROM dual) BETWEEN 
            SYSDATE - 10 AND SYSDATE+10 ) > 0