增量日期ORA - 00932. 00000 - “不一致的数据类型:预期%s获得%s”

时间:2016-12-14 13:32:10

标签: sql oracle date sqlplus

我的脚本中有一个已定义的日期

define REPORT_DATE = to_date('05112016','ddmmyyyy')

当我尝试增加它时

select *
from STG_PREP_CHARGES_PLAN SPCP
where &REPORT_DATE + 1 < SPCP.NEXT_CHARGE_DATE

我得到ORA - 00932. 00000 - “不一致的数据类型:预期%s得到%s”

是否有类似add_months的功能,但是几天?

SPCP.NEXT_CHARGE_DATE是一个日期

desc STG_PREP_CHARGES_PLAN
Name                   Null Type          
---------------------- ---- ------------- 
NEXT_CHARGE_DATE            DATE          

更新

很奇怪。我将日期更改为to_date('06112016','ddmmyyyy')然后返回并开始工作。不知道它是什么。

UPDATE2 发现问题所在。我只需在运行之前选择所有查询。

1 个答案:

答案 0 :(得分:2)

这里的问题似乎是NEXT_CHARGE_DATE

的类型

例如,如果我针对date检查类似你的变量,我没有问题:

SQL> define REPORT_DATE = to_date('05112016','ddmmyyyy');
SQL> select 1 from dual where sysdate = &report_date;
old   1: select 1 from dual where sysdate = &report_date
new   1: select 1 from dual where sysdate = to_date('05112016','ddmmyyyy')

no rows selected

无论我是否在变量值中添加了一天,它仍然有效:

SQL> select 1 from dual where sysdate = &report_date +1;
old   1: select 1 from dual where sysdate = &report_date +1
new   1: select 1 from dual where sysdate = to_date('05112016','ddmmyyyy') +1

no rows selected

如果我尝试将变量与不是date的对象进行比较,比如说number,我就会出错:

SQL> select 1 from dual where 1 = &report_date +1;
old   1: select 1 from dual where 1 = &report_date +1
new   1: select 1 from dual where 1 = to_date('05112016','ddmmyyyy') +1
select 1 from dual where 1 = to_date('05112016','ddmmyyyy') +1
                           *
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE

无论我删除+1,我仍然会收到相同的错误,因为问题在于您要比较的对象的类型,而不是您在变量中添加一天的方式:

SQL> select 1 from dual where 1 = &report_date;
old   1: select 1 from dual where 1 = &report_date
new   1: select 1 from dual where 1 = to_date('05112016','ddmmyyyy')
select 1 from dual where 1 = to_date('05112016','ddmmyyyy')
                           *
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE

要解决此问题,您应该检查NEXT_CHARGE_DATE的类型,并在代码中添加一些to_charto_numberto_date来比较相同类型的对象。

使用date列,一切正常(Oracle Database 11g Express Edition版本11.2.0.2.0 - 64位生产):

SQL> create table STG_PREP_CHARGES_PLAN ( NEXT_CHARGE_DATE date);

Table created.

SQL> define REPORT_DATE = to_date('05112016','ddmmyyyy');
SQL> select *
  2  from STG_PREP_CHARGES_PLAN SPCP
  3  where &REPORT_DATE + 1 < SPCP.NEXT_CHARGE_DATE;
old   3: where &REPORT_DATE + 1 < SPCP.NEXT_CHARGE_DATE
new   3: where to_date('05112016','ddmmyyyy') + 1 < SPCP.NEXT_CHARGE_DATE

no rows selected