简体   繁体   English

日期值无效的Oracle案例声明

[英]Oracle Case Statement with date value not a valid month

I have the below case statement but I would like to add on to the case statement where END_DT is 1/1/3000 classify as Open. 我有以下case语句,但我想添加到END_DT为1/1/3000分类为Open的case语句。

CASE WHEN x.END_dt IS NOT NULL OR REASON IS NOT NULL 
THEN 'Closed' ELSE 'Open' END AS Enrolled

I'm getting the below results but in this case this should be classified as Open under the Enrolled column 我得到以下结果,但在这种情况下,应在“已注册”列下将其分类为“打开”

I've tried adding to the Case Statment but I get errors. 我尝试添加到“案例陈述”中,但出现错误。

One - inconsistent datatype; 一种-数据类型不一致; expected Date got number Two - When add '1/1/3000' to the date - Not a valid month. 预期日期为第二个-在日期中添加“ 1/1/3000”时-无效的月份。

Help would be appreciated. 帮助将不胜感激。

Table

 id  End_dt          Reason  Enrolled
  1    1/1/3000               Closed

Thanks to mathguy for reminding me about the ANSI standard date format. 感谢mathguy提醒我有关ANSI标准日期格式的信息。 I changed my example so assuming x.END_dt is a DATE datatype: 我更改了示例,因此假设x.END_dt是DATE数据类型:

  CASE
    WHEN x.END_dt = date '3000-01-01'
      THEN 'Open'
    WHEN x.END_dt IS NULL AND REASON IS NULL
      THEN 'Open'
    WHEN x.END_dt IS NOT NULL OR REASON IS NOT NULL
      THEN 'Closed'
    ELSE 'Unexpected x.END_dt value: ' || to_char(x.END_dt)
  END AS Enrolled

I also tightened up the case statement to handle an unexpected END_dt value and explicitly test conditions on their own 'WHEN' lines. 我还收紧了case语句,以处理意外的END_dt值,并在其自己的“ WHEN”行中明确测试条件。 I like to do this as this way the intention of the test is clear, it's clear which expression is being testing when stepping through a debugger (like Toad), and when the clients inevitably want to change things its easy to move a line around or change the order of tests when they are on their own lines. 我喜欢这样做,因为这样可以明确测试的意图,很清楚在调试器(例如Toad)中逐步调试哪个表达式,并且客户端不可避免地要更改内容时,可以轻松地移动一行或当它们独立运行时更改测试的顺序。

To avoid any reliance on NLS_DATE_FORMAT , one can use the ANSI standard date format (which is supported by Oracle): WHEN x.end_dt = date '3000-01-01' THEN ... . 为了避免对NLS_DATE_FORMAT依赖,可以使用ANSI标准日期格式(Oracle支持): WHEN x.end_dt = date '3000-01-01' THEN ... The whole CASE expression can be written like so: 整个CASE表达式可以这样写:

CASE
  WHEN x.end_dt = date '3000-01-01' OR (x.end_dt IS NULL AND x.reason IS NULL)
    THEN 'Open'
  ELSE   'Closed'
END AS enrolled

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM