Where 子句中的案例日期过滤器 - Oracle

Case Date Filter in Where Clause - Oracle

我正在尝试 return 一个取决于 current_date 的日期范围。逻辑如下:

如果当前月份是一月,则 return 是上一年的年初;否则 return 当前年份的开始。

附加代码不断抛出语法错误。我在 Alteryx 中使用 NS 64 位 ODBC 来提取信息。

代码:

and t.TRANDATE between

        --period beg
        (case
            when to_char(current_date, 'MM')='01' then add_months(trunc(current_date, 'YYYY'),-12)
            else trunc(current_date, 'YYYY')
        end)
    and
        --period end
        trunc(current_date,'MM')-1/84600)

关于如何清除语法错误或重组语句的任何建议?

语法错误? 哪个语法错误?我没有得到:

SQL> select *
  2  from dual
  3  where sysdate not between
  4    case when to_char(current_date, 'mm') = '01' then add_months(trunc(current_date, 'YYYY'),-12)
  5         else trunc(current_date, 'yyyy')
  6    end
  7    and trunc(current_date, 'mm') - 1/84600;

D
-
X

SQL>

(我使用 NOT BETWEENDUAL table 因为我没有你的 table(s),只是为了证明这段代码可以正常工作) .

您可以使用:

AND t.TRANDATE >= TRUNC(ADD_MONTHS(current_date, -1), 'YYYY')
AND t.TRANDATE <  TRUNC(current_date,'MM')