Solved

How to use case in insert into select statement?

Posted on 2014-11-20
8
421 Views
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)

BR
0
Comment
Question by:GurcanK
  • 4
  • 3
8 Comments
 
LVL 73

Expert Comment

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

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
Comment Utility
>>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.
0
 
LVL 73

Accepted Solution

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

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
Comment Utility
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.
0
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

by:sdstuber
Comment Utility
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
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
Version:  11.2.0.2:

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;
XXX
0
 

Author Comment

by:GurcanK
Comment Utility
Thanks all, fortunately data-types are the same.
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
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

SELECT DECODE(1, 1, 'XXX', 1) FROM DUAL;


and thanks again!
0

Featured Post

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.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

763 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

10 Experts available now in Live!

Get 1:1 Help Now