Oracle Conditional Insert Statement

Dear Experts,

I would like to insert data from one table to another with some conditions.

Think that we have table-A(a,b,c) and table-B(x,y,z). I will do  "insert into Table-A select x,y,z from Table_B" but with the conditions: if x='Sigma' the y will be x (the inserted value will be same as field x), if x='Teta' the y will be character value 'Beta' while inserting.  

Best Regards
GurcanKAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
A case statement should work.

something like:
insert into tableA(a,b,c) select x, case x when 'Sigma' then y when 'Teta' then 'Invalid', x from tableB;
sdstuberCommented:
extending on what slightwv posted with some small corrections

INSERT INTO tablea(a, b, c)
     SELECT x, CASE x WHEN 'Sigma' THEN x WHEN 'Teta' THEN 'Beta' END, z
       FROM tableb;


You didn't specify what should happen if x is NOT either Sigma nor Teta.
The query above would return NULL for those values.
If the CASE should default to "y" in that scenario then try this instead

INSERT INTO tablea(a, b, c)
    SELECT x,
           CASE x WHEN 'Sigma' THEN x
                  WHEN 'Teta' THEN 'Beta'
                  ELSE y
           END,
           z
      FROM tableb;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.