ORA-01861:文字与oracle中case语句的格式字符串不匹配

时间:2018-09-07 08:03:09

标签: oracle date datetime

请帮助我纠正此错误,我不断收到此错误

  

ORA-01861:文字与以下案例中的case语句的格式字符串不匹配   甲骨文

SELECT activity_made,
       (CASE
            WHEN (TO_DATE(activity_made, 'DD-Mon-YYYY')) = TRUNC(SYSDATE) THEN TO_CHAR(activity_made, 'hh12:mi PM')
            WHEN TRUNC(TO_DATE(activity_made, 'MM-DD-YYYY')) BETWEEN TRUNC(SYSDATE, 'yy') AND TRUNC(SYSDATE - 1) THEN TO_CHAR(activity_made, 'Mon dd')
            ELSE TO_CHAR(TO_DATE(activity_made, 'MM/DD/YYYY'), 'mm/dd/yyyy')
        END)
           AS actmode
  FROM (SELECT (CASE
                    WHEN (SELECT COUNT(*)
                            FROM sfa_activity sa
                           WHERE     sa.companyid = opp.companyid
                                 AND sa.opptyid = opp.opptyid
                                 AND sa.TYPE = 'NOTE') > 0
                    THEN
                        (SELECT NVL(MAX(TO_CHAR(sa.updat, 'YYYY/MM/DD HH24:MI:SS')), '0000/00/00 00:00:00')
                           FROM sfa_activity sa
                          WHERE     sa.companyid = opp.companyid
                                AND sa.opptyid = opp.opptyid
                                AND sa.TYPE = 'NOTE')
                    ELSE
                        (SELECT NVL(MAX(TO_CHAR(sa.updat, 'YYYY/MM/DD HH24:MI:SS')), '1001/01/01 01:01:01')
                           /*ELSE (SELECT (MAX(TO_CHAR(sa.updat,'YYYY/MM/DD HH24:MI:SS')))*/
                           FROM sfa_activity sa
                          WHERE     sa.companyid = opp.companyid
                                AND sa.opptyid = opp.opptyid
                                AND sa.TYPE <> 'NOTE')
                END)
                   AS activity_made
          FROM sfa_opportunities opp
         WHERE companyid = 1192)

3 个答案:

答案 0 :(得分:0)

ACTIVITY_MADE的格式似乎为yyyy/mm/dd hh24:mi:ss(如内联视图所建议)。如果简化,则查询看起来像

select to_date(activity_made, ...) --> should use the same format as below, in an inline view
from (select activity_made         -->TO_CHAR(sa.updat, 'YYYY/MM/DD HH24:MI:SS')
      from sfa_opportunities) 

因此:如果您为activity_made使用了该格式掩码,则应在 main TO_CHAR中为SELECT使用该格式掩码。但是,您使用

  • dd-mon-yyyy
  • mm-dd-yyyy
  • mm / dd / yyyy

yyyy/mm/dd hh24:mi:ss中切换到CASE

答案 1 :(得分:0)

我建议您将查询重写为以下内容:

SELECT sa.updat activity_made
     , CASE WHEN TRUNC(sa.updat) = TRUNC(SYSDATE) THEN TO_CHAR(sa.updat, 'hh12:mi PM')
            WHEN sa.updat > TRUNC(SYSDATE,'yy') THEN TO_CHAR(sa.updat, 'Mon dd')
            ELSE TO_CHAR(sa.updat, 'mm/dd/yyyy')
       END actmode
  FROM sfa_activity sa
 WHERE (sa.companyid, sa.opptyid) IN (SELECT opp.companyid, opp.opptyid 
                                        FROM sfa_opportunities opp
                                       WHERE companyid = 1192)
 ORDER BY CASE WHEN sa.TYPE = 'NOTE' THEN 0 ELSE 1 END, sa.updat DESC
 FETCH first ROW ONLY 

您的版本有一些缺点:

  • 不必要的类型转换
  • 在同一张桌子上有很多不需要的选择
  • 可能还有一些性能问题

不进行任何活动时的处理应在外部而不是内部选择中进行。 (使维护更容易。)

答案 2 :(得分:0)

  

谢谢大家,我通过在activity_made中应用案例获得了解决方案

/* Formatted on 2018/09/10 14:15 (Formatter Plus v4.8.5) */
SELECT activity_made,
       (CASE
           WHEN activity_made = '0000/00/00 00:00:00'
              THEN NULL
           ELSE activity_made
        END
       ) AS actmode
  FROM (SELECT (CASE
                   WHEN (SELECT COUNT (*)
                           FROM sfa_activity sa
                          WHERE sa.companyid = opp.companyid
                            AND sa.opptyid = opp.opptyid
                            AND sa.TYPE = 'NOTE') > 0
                      THEN (SELECT NVL
                                      (MAX
                                          (TO_CHAR
                                              (CASE
                                                  WHEN TRUNC (sa.updat) =
                                                               TRUNC (SYSDATE)
                                                     THEN TO_CHAR
                                                                 (sa.updat,
                                                                  'hh12:mi PM'
                                                                 )
                                                  WHEN TRUNC (sa.updat)
                                                         BETWEEN TRUNC
                                                                     (SYSDATE,
                                                                      'yy'
                                                                     )
                                                             AND TRUNC
                                                                    (  SYSDATE
                                                                     - 1
                                                                    )
                                                     THEN TO_CHAR (sa.updat,
                                                                   'Mon dd'
                                                                  )
                                                  ELSE TO_CHAR (sa.updat,
                                                                'DD/MM/YYYY'
                                                               )
                                               END
                                              )
                                          ),
                                       '0000/00/00 00:00:00'
                                      )
                              FROM sfa_activity sa
                             WHERE sa.companyid = opp.companyid
                               AND sa.opptyid = opp.opptyid
                               AND sa.TYPE = 'NOTE')
                   ELSE (SELECT NVL
                                   (MAX
                                       (TO_CHAR
                                           (CASE
                                               WHEN TRUNC (sa.updat) =
                                                               TRUNC (SYSDATE)
                                                  THEN TO_CHAR (sa.updat,
                                                                'hh12:mi PM'
                                                               )
                                               WHEN TRUNC (sa.updat)
                                                      BETWEEN TRUNC (SYSDATE,
                                                                     'yy'
                                                                    )
                                                          AND TRUNC (  SYSDATE
                                                                     - 1
                                                                    )
                                                  THEN TO_CHAR (sa.updat,
                                                                'Mon dd'
                                                               )
                                               ELSE TO_CHAR (sa.updat,
                                                             'DD/MM/YYYY'
                                                            )
                                            END
                                           )
                                       ),
                                    '0000/00/00 00:00:00'
                                   )
                           /*ELSE (SELECT (MAX(TO_CHAR(sa.updat,'YYYY/MM/DD HH24:MI:SS')))*/
                         FROM   sfa_activity sa
                          WHERE sa.companyid = opp.companyid
                            AND sa.opptyid = opp.opptyid
                            AND sa.TYPE <> 'NOTE')
                END
               ) AS activity_made
          FROM sfa_opportunities opp
         WHERE companyid = 1192)