How to use case in insert into select statement?

Posted on 2014-11-20
Last Modified: 2014-11-20
Dear Experts,

Is below statement is true or can you offer an alternative?

Table TA (x,y,z,t)
Table TB (a,b,c,d)

insert into TA (x,y,z,t) select a,b,c,(case when c=1 then 'XXX' else TB.d end)

Please be careful that if c=1 then assign constant value 'XXX' else assign value of column d)

Question by:GurcanK
  • 4
  • 3
LVL 73

Expert Comment

ID: 40455218
insert into TA (x,y,z,t) select a,b,c,(case when c=1 then 'XXX' else d end) from tb
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
ID: 40455221
>>insert into TA (x,y,z,t) select a,b,c,(case when c=1 then 'XXX' else TB.d end)

you are missing the FROM TB after the select.

Just like the last two questions:  Looks good.

Just set up some test tables and try it.
LVL 73

Accepted Solution

sdstuber earned 250 total points
ID: 40455224
insert into TA (x,y,z,t) select a,b,c, decode(c,1,'XXX',d) from tb
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
ID: 40455230
Missed something:
then 'XXX' else TB.d

Since 'XXX' is a string, TB.d will need to be a varcahr2 or similar string variable.  if it is a number or a date, you will likely get an error.  You will need to convert TB.d to a string datattype by using TO_CHAR and the appropriate formatting.
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

LVL 73

Expert Comment

ID: 40455241
actually, since 'XXX' is a string, and it's the first return result in the CASE, it's determining the data type of the rest of the case results,  so anything else will be implicitly converted to text (char)

so, you probably won't get an error, but you might get funny results if your data converts into a format you don't want.

So, TO_CHAR with an explicit format for non-text data is a good idea.

Same applies to the DECODE alternative
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40455246

SQL> select case when 1=1 then 'XXX' else 1 end from dual
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected CHAR got NUMBER

SQL> select case when 1=1 then 'XXX' else to_char(1) end from dual;

Author Comment

ID: 40455256
Thanks all, fortunately data-types are the same.
LVL 73

Expert Comment

ID: 40455267
interesting - I stand corrected.  Thanks!

I know I've seen the first element determine data types before.  Maybe it was a bug in older versions.

Does work with decode though


and thanks again!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
This post first appeared at Oracleinaction  ( Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now