Solved

How to use case in insert into select statement?

Posted on 2014-11-20
8
451 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 74

Expert Comment

by:sdstuber
ID: 40455218
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
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.
0
 
LVL 74

Accepted Solution

by:
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
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 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.
0
 
LVL 74

Expert Comment

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

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40455246
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
ID: 40455256
Thanks all, fortunately data-types are the same.
0
 
LVL 74

Expert Comment

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

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


and thanks again!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

856 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