2013-12-10 100 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