2013-12-10 38 views
0

在Oracle中,我需要设置MYDATE的where子句取决于当前月份的月份。 如果month等于或大于4月,则将MYDATE限制为当前年份(01.01.YEAR - 31.12.YEAR) ELSE将MYDATE限制为比当前时间早的日期。Oracle - where子句中的复杂case case语句

我需要一个纯粹的SQL解决方案,如果可能的话不需要PL/SQL。我试着去适应我在网上,并在stackoverflow.com发现,但语法是错误的:

SELECT text, mydate 
FROM mytable 
WHERE 
CASE 
    WHEN to_number(TO_CHAR(sysdate, 'MM')) >= 4 -- if month of current year >= april 
     THEN mydate >=TRUNC(LAST_DAY(SYSDATE), 'YYYY') --beginning of current year 
     AND mydate < TRUNC(LAST_DAY(SYSDATE)+1, 'YYYY') -- end of current year 
    ELSE -- if month of current year < april 
     mydate < sysdate; 

谁能帮我在这里?

非常感谢您提前。

回答

2

你不需要CASE。只需在WHERE中执行AND/OR即可。

SELECT text, mydate 
FROM mytable 
WHERE 
    ( to_number(TO_CHAR(sysdate, 'MM')) >= 4 -- if month of current year >= april 
    AND mydate >=TRUNC(LAST_DAY(SYSDATE), 'YYYY') --beginning of current year 
    AND mydate < TRUNC(LAST_DAY(SYSDATE)+1, 'YYYY') -- end of current year 
    ) OR (
     to_number(TO_CHAR(sysdate, 'MM')) < 4 
    AND mydate < sysdate) 
    ); 
+0

确实!谢谢! – royskatt