Oracle datatype error

孤街浪徒 提交于 2019-12-11 19:16:10

问题


I'm trying to insert a value into date datatype by selecting a value from a source table whose column is also date datatype. I have selected the column directly without doing any conversion using to_date function, because both are same types but I'm getting the following error:

SQL Error: ORA-00932: inconsistent datatypes: expected DATE got NUMBER
00932. 00000 -  "inconsistent datatypes: expected %s got %s"

I had doubled checked, source column has no null values.

insert into Target(Targetdate) 
  select to_date(Source.START_DATE,'yyyy-mm-dd')
    from Source

Thanks for looking into this.


回答1:


I assume you try to get the dates truncated to the day into the Target table. This can be done by doing so:

insert into Target(Targetdate) select trunc(Source.START_DATE,'DD') from Source

EDIT

Dazzal mentioned that as this is the default operation when leaving out the second parameter to TRUNC, so this is even a bit simpler:

insert into Target(Targetdate) select trunc(Source.START_DATE) from Source

The problem with your query was that Source.START_DATE is a date, not a string...

EDIT2

As it seems that you want to get date strings in a specific format - which has nothing to do with the date type, and how thigs are stored internally - just do this:

SELECT to_char(START_DATE,'YYYY-MM-DD' from Source;
  • Oracle TRUNC



回答2:


You said you are selecting a date column from source table. When it's date column, then why are you converting again into date? Of course it will give error also..

insert into Target(Targetdate) 
select to_date(Source.START_DATE,'yyyy-mm-dd'), from Source

Input parameter of 'to_date' function is 'string'

to_date( string1, [ format_mask ], [ nls_language ] )

UPDATE1:

alter session set nls_date_format = 'yyyy-mm-dd';


insert into Target(Targetdate) 
select to_char(Source.START_DATE,'yyyy-mm-dd') from Source



回答3:


"example of my source value is 11-JUN-13 and i need target value as YYYY-MM-DD how can i achieve this? "

All Oracle dates are stored in the same internal format:

SQL>  select dump(sysdate) from dual;

DUMP(SYSDATE)
---------------------------------------------
Typ=13 Len=8: 221,7,1,3,10,22,8,0

SQL>     

The display date is entirely dependent on the client environment's default values, or the format mask if we're using TO_CHAR().

If both your columns are the DATE datatype you have overcomplicated things: you do not need to cast the values in the INSERT.

insert into Target (Targetdate) 
select Source.START_DATE
from Source

You just need to apply a format mask when selecting the target date from Target.



来源:https://stackoverflow.com/questions/14142019/oracle-datatype-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!