user1751356
user1751356

Reputation: 615

Oracle datatype error

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.

Upvotes: 0

Views: 875

Answers (3)

ppeterka
ppeterka

Reputation: 20726

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;

Upvotes: 2

APC
APC

Reputation: 146209

"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.

Upvotes: 1

Mariappan Subramanian
Mariappan Subramanian

Reputation: 10063

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

Upvotes: 1

Related Questions