在 sql oracle 中的情况

Case when in sql oracle

我是 sql oracle 的新手,我对如何在这里使用 case 有疑问,例如,就是这种情况:

  1. if transaction_type RECEIVE then receipt date - promised date

  2. 如果收货日期 > 承诺日期则收货日期 - 承诺日期

  3. 如果收货日期 <= 承诺日期则为空

  4. 如果尚未收到或transaction_type DELIVER 则 sysdate - 承诺日期

    CASE WHEN transaction_type ='RECEIVE' THEN to_char(creation_date, 'DD-MON-YYYY') – to_char(promised_date, 'DD-MON-YYYY')
      WHEN to_char(creation_date, 'DD-MON-YYYY') > to_char(promised_date, 'DD-MON-YYYY') THEN to_char(creation_date, 'DD-MON-YYYY') – to_char(promised_date, 'DD-MON-YYYY')
      WHEN to_char(creation_date, 'DD-MON-YYYY') <= to_char(promised_date, 'DD-MON-YYYY') THEN null
      WHEN transaction_type ='DELIVER' THEN to_char(sysdate, 'DD-MON-YYYY') - to_char(promised_date, 'DD-MON-YYYY') END AS delay_day
    

ORA-00911: invalid character 00911. 00000 - "invalid character" *Cause: The identifier name started with an ASCII character other than a letter or a number. After the first character of the identifier name, ASCII characters are allowed including "$", "#" and "_". Identifiers enclosed in double quotation marks may contain any character other than a double quotation. Alternate quotation marks (q'#...#') cannot use spaces, tabs, or carriage returns as delimiters. For all other contexts, consult the SQL Language Reference Manual. *Action: Check the Oracle identifier naming convention. If you are attempting to provide a password in the IDENTIFIED BY clause of a CREATE USER or ALTER USER statement, then it is recommended to always enclose the password in double quotation marks because characters other than the double quotation are then allowed. Error at Line: 21 Column: 94

这是错误,我不知道这有什么问题

看起来你 copy/pasted 代码来自某些 fancy 文本编辑器(也许是 MS Word?),它使用 fancy 字符对于单引号和双引号,以及 minus 符号。如果转换为“正常”减-,那么它应该就可以了:

      CASE
          WHEN transaction_type = 'RECEIVE'
          THEN
               TO_CHAR (creation_date, 'DD-MON-YYYY')
             - TO_CHAR (promised_date, 'DD-MON-YYYY')         --> this minus
          WHEN TO_CHAR (creation_date, 'DD-MON-YYYY') >
               TO_CHAR (promised_date, 'DD-MON-YYYY')
          THEN
               TO_CHAR (creation_date, 'DD-MON-YYYY')
             - TO_CHAR (promised_date, 'DD-MON-YYYY')         --> and this minus
          WHEN TO_CHAR (creation_date, 'DD-MON-YYYY') <=
               TO_CHAR (promised_date, 'DD-MON-YYYY')
          THEN
             NULL
          WHEN transaction_type = 'DELIVER'
          THEN
               TO_CHAR (SYSDATE, 'DD-MON-YYYY')
             - TO_CHAR (promised_date, 'DD-MON-YYYY')
       END AS delay_day

然而:你希望得到什么结果?减去两个 字符串 是荒谬的。什么是 'A' - '23FX__#'?如果您想减去 日期 ,当然 - 为什么不 - 结果将是这两个日期之间的天数。

此外,在大多数情况下,比较字符串也不起作用。

这意味着您应该 - 或许 - 完全删除 TO_CHAR。

        CASE
          WHEN transaction_type = 'RECEIVE'
          THEN
             creation_date - promised_date
          WHEN creation_date > promised_date
          THEN
             creation_date - promised_date
          WHEN creation_date <= promised_date
          THEN
             NULL
          WHEN transation_type = 'DELIVER'
          THEN
             SYSDATE - promised_date
       END AS delay_day