Sql ORA-00932:不一致的数据类型:在将1添加到日期时应为CHAR GET NUMBER

Sql ORA-00932:不一致的数据类型:在将1添加到日期时应为CHAR GET NUMBER,sql,oracle,oracle11g,Sql,Oracle,Oracle11g,可能是个愚蠢的错误,但我自己也没能弄明白。当我在Oracle 11g中运行此查询时 如果这个问题的答案是这样的,请让我知道链接 with LAST_BUSINESS_DAY as (select DECODE(to_char(last_day(to_date('29-mar-2013')), 'D'), , '7', to_char((last_day('29-mar-2013') - 1), 'DD-MON-YYYY')

可能是个愚蠢的错误,但我自己也没能弄明白。当我在Oracle 11g中运行此查询时

如果这个问题的答案是这样的,请让我知道链接

with LAST_BUSINESS_DAY as (select DECODE(to_char(last_day(to_date('29-mar-2013')), 'D'), 
                                  , '7', to_char((last_day('29-mar-2013') - 1), 'DD-MON-YYYY')
                                  , '1', to_char((last_day('29-mar-2013') - 2), 'DD-MON-YYYY')
                                  , to_char(last_day('29-apr-2013'), 'DD-MON-YYYY')) as LAST_BD from dual),
      HOLIDAYS as (select distinct rpt_day
                     from rpt_days rpt left join
                          calendars cal on rpt.calendar_id = cal.calendar_id
                    where rpt.type = 2 
                      and cal.group = 4)
  select case when to_char(to_date(LAST_BD, 'DD-MON-YYYY'), 'D') is null
              then LAST_BD
              else DECODE(to_char(to_date(LAST_BD, 'DD-MON-YYYY') , 'D')
                          , '6', LAST_BD
                          , '2', LAST_BD
                          , LAST_BD)
         end as LAST_BD_OF_MONTH
    from LAST_BUSINESS_DAY LBD 
         inner join HOLIDAYS H on LBD.LAST_BD = H.rpt_day
我得到的结果是

LAST_BD_OF_MONTH
===================
29-MAR-2013
现在,当我试图在
LAST_BD
日期中添加一天时,它抛出了一个错误

with LAST_BUSINESS_DAY as (select DECODE(to_char(last_day(to_date('29-mar-2013')), 'D'), 
                                  , '7', to_char((last_day('29-mar-2013') - 1), 'DD-MON-YYYY')
                                  , '1', to_char((last_day('29-mar-2013') - 2), 'DD-MON-YYYY')
                                  , to_char(last_day('29-apr-2013'), 'DD-MON-YYYY')) as LAST_BD from dual),
      HOLIDAYS as (select distinct rpt_day
                     from rpt_days rpt left join
                          calendars cal on rpt.calendar_id = cal.calendar_id
                    where rpt.type = 2 
                      and cal.group = 4)
  select case when to_char(to_date(LAST_BD, 'DD-MON-YYYY'), 'D') is null
              then LAST_BD
              else DECODE(to_char(to_date(LAST_BD, 'DD-MON-YYYY') , 'D') -- line 35
                          , '6', LAST_BD - 1 -- CHANGED THIS
                          , '2', LAST_BD + 1 -- CHANGED THIS
                          , LAST_BD)
         end as LAST_BD_OF_MONTH
    from LAST_BUSINESS_DAY LBD 
         inner join HOLIDAYS H on LBD.LAST_BD = H.rpt_day
错误消息

ORA-00932:不一致的数据类型:预期字符数
93200000-“不一致的数据类型:应为%s获得%s” *原因:
*操作:第35行第20列出现错误

正如我所说,这可能是我这边的一个简单疏忽。我尝试将
上一次的BD
转换为
日期,但没有成功

我试着改变
解码
,如下所示

case when to_char(to_date(LAST_BD, 'DD-MON-YYYY'), 'D') is null
     then LAST_BD
     else DECODE(to_char(to_date(LAST_BD, 'DD-MON-YYYY') , 'D')
                 , '6', to_date(LAST_BD, 'DD-MON-YYYY') - 1
                 , '2', LAST_BD + 1 -- line 37
                 , LAST_BD)
end as LAST_BD_OF_MONTH
得到了这个错误:

ORA-00932:不一致的数据类型:预期日期获取编号
93200000-“不一致的数据类型:应为%s获得%s” *原因:
*操作:第37行第42列出现错误

因此,我将
第37行更改为

case when to_char(to_date(LAST_BD, 'DD-MON-YYYY'), 'D') is null
     then LAST_BD
     else DECODE(to_char(to_date(LAST_BD, 'DD-MON-YYYY') , 'D')
                , '6', to_date(LAST_BD, 'DD-MON-YYYY') - 1
                , '2', to_date(LAST_BD, 'DD-MON-YYYY') + 1
                , LAST_BD)
end as LAST_BD_OF_MONTH
这一次是另一个信息

ORA-00932:不一致的数据类型:预期的字符获取日期
93200000-“不一致的数据类型:应为%s获得%s” *原因:
*操作:第35行第20列出现错误

非常感谢您为纠正此错误提供的任何帮助

回答

with LAST_BUSINESS_DAY as (select DECODE(to_char(last_day(to_date('29-mar-2013')), 'D'), 
                                  , '7', to_char((last_day('29-mar-2013') - 1), 'DD-MON-YYYY')
                                  , '1', to_char((last_day('29-mar-2013') - 2), 'DD-MON-YYYY')
                                  , to_char(last_day('29-apr-2013'), 'DD-MON-YYYY')) as LAST_BD from dual),
      HOLIDAYS as (select distinct rpt_day
                     from rpt_days rpt left join
                          calendars cal on rpt.calendar_id = cal.calendar_id
                    where rpt.type = 2 
                      and cal.group = 4)
  select case when to_char(to_date(LAST_BD, 'DD-MON-YYYY'), 'D') is null
              then LAST_BD
              else DECODE(to_char(to_date(LAST_BD, 'DD-MON-YYYY') , 'D')
                          , '6', to_char (to_date(LAST_BD, 'DD-MON-YYYY') - 1, 'DD-MON-YYYY')
                          , '2', to_char (to_date(LAST_BD, 'DD-MON-YYYY') + 1, 'DD-MON-YYYY')
                          , LAST_BD)
         end as LAST_BD_OF_MONTH
    from LAST_BUSINESS_DAY LBD 
         inner join HOLIDAYS H on LBD.LAST_BD = H.rpt_day

问题是DECODE需要某种CHAR参数,并且
LAST\u BD+1
甚至
TO\u DATE(LAST\u BD…
分别返回一个数字和一个日期

下面的SQL FIDLE演示了如何修复此问题

以下是查询:

with LAST_BUSINESS_DAY as (select DECODE(to_char(last_day(to_date('29-mar-2013')), 'D')
                                  , '7', to_char((last_day('29-mar-2013') - 1), 'DD-MON-YYYY')
                                  , '1', to_char((last_day('29-mar-2013') - 2), 'DD-MON-YYYY')
                                  , to_char(last_day('29-apr-2013'), 'DD-MON-YYYY')) as LAST_BD from dual),
      HOLIDAYS as (select distinct reporting_day
                     from tbm_reporting_days trdy left join
                          tbm_calendars tcal on trdy.calendar_id = tcal.calendar_id
                    where trdy.type = 2 
                      and tcal.site_id = 4)
  select case when to_char(to_date(LAST_BD, 'DD-MON-YYYY'), 'D') is null
              then LAST_BD
              else DECODE(to_char(to_date(LAST_BD, 'DD-MON-YYYY') , 'D')
                          , '6', TO_CHAR(
                              (TO_DATE(LAST_BD, 'DD-MON-YYYY') - 1), 'D')
                          , '2', TO_CHAR(
                              (TO_DATE(LAST_BD, 'DD-MON-YYYY') - 1), 'D')
                          , LAST_BD)
         end as LAST_BD_OF_MONTH
    from LAST_BUSINESS_DAY LBD 
         inner join HOLIDAYS H on LBD.LAST_BD = H.reporting_day

您必须将数字或日期转换回带有to_CHAR的字符。

因此,我认为您正确地将
最后一个\u BD
VARCHAR2
转换为
日期(由于):

在第二个查询中,您尝试从此
VARCHAR2
中减去
1

LAST_BD - 1
这不起作用。因此,您会得到以下错误:

ORA-00932: inconsistent datatypes: expected CHAR got NUMBER
如果将其转换为
DATE
,则添加
1
,然后将其转换回
VARCHAR2

with LAST_BUSINESS_DAY as (select DECODE(to_char(last_day(to_date('29-mar-2013')), 'D'), 
                                  , '7', to_char((last_day('29-mar-2013') - 1), 'DD-MON-YYYY')
                                  , '1', to_char((last_day('29-mar-2013') - 2), 'DD-MON-YYYY')
                                  , to_char(last_day('29-apr-2013'), 'DD-MON-YYYY')) as LAST_BD from dual),
      HOLIDAYS as (select distinct rpt_day
                     from rpt_days rpt left join
                          calendars cal on rpt.calendar_id = cal.calendar_id
                    where rpt.type = 2 
                      and cal.group = 4)
  select case when to_char(to_date(LAST_BD, 'DD-MON-YYYY'), 'D') is null
              then LAST_BD
              else DECODE(to_char(to_date(LAST_BD, 'DD-MON-YYYY') , 'D') -- line 35
                     , '6', to_char (to_date(LAST_BD, 'DD-MON-YYYY') - 1, 'DD-MON-YYYY') 
                     , '2', to_char (to_date(LAST_BD, 'DD-MON-YYYY') + 1, 'DD-MON-YYYY') 
                          , LAST_BD)
         end as LAST_BD_OF_MONTH
    from LAST_BUSINESS_DAY LBD 
         inner join HOLIDAYS H on LBD.LAST_BD = H.rpt_day

请注意,需要转换回
VARCHAR2
,因为
DECODE
只允许一种类型的值。

Oracle似乎要求case构造所有表达式的类型,然后表达式与第一个之后的表达式类型一致

没有执行类型转换。

但我需要的是日期,而不是日期ordinal@SaagarEliasJacky明白了,我只是想帮你理解这个错误。谢谢……这很有效。只需提一下它是
to_char(to_date(LAST_BD,'DD-MON-YYYY')-1,'DD-MON-YYYY')
,而不是
to_char(to_date(LAST_BD,'DD-MON-YYYY')-1),'DD-MON-yyyyyyy'))
这是对错误的简要说明和原因。基本上,您提到的值的数据类型必须在相同级别的所有后续值中相同。谢谢,用户2038893。救了我一天。
with LAST_BUSINESS_DAY as (select DECODE(to_char(last_day(to_date('29-mar-2013')), 'D'), 
                                  , '7', to_char((last_day('29-mar-2013') - 1), 'DD-MON-YYYY')
                                  , '1', to_char((last_day('29-mar-2013') - 2), 'DD-MON-YYYY')
                                  , to_char(last_day('29-apr-2013'), 'DD-MON-YYYY')) as LAST_BD from dual),
      HOLIDAYS as (select distinct rpt_day
                     from rpt_days rpt left join
                          calendars cal on rpt.calendar_id = cal.calendar_id
                    where rpt.type = 2 
                      and cal.group = 4)
  select case when to_char(to_date(LAST_BD, 'DD-MON-YYYY'), 'D') is null
              then LAST_BD
              else DECODE(to_char(to_date(LAST_BD, 'DD-MON-YYYY') , 'D') -- line 35
                     , '6', to_char (to_date(LAST_BD, 'DD-MON-YYYY') - 1, 'DD-MON-YYYY') 
                     , '2', to_char (to_date(LAST_BD, 'DD-MON-YYYY') + 1, 'DD-MON-YYYY') 
                          , LAST_BD)
         end as LAST_BD_OF_MONTH
    from LAST_BUSINESS_DAY LBD 
         inner join HOLIDAYS H on LBD.LAST_BD = H.rpt_day