Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Add values from Col 5 to values in col 6 and put them into col 7 in the SAME oracle Table

Posted on 2016-08-22
Medium Priority
63 Views
Last Modified: 2016-08-23
Hi experts,

I have a table Alpha, it has col_1, Col_2, Col_3, Col_4, Col_5, Col_6, Col_7. The datatype in each column is number (10).

I want to add values in Col_5 with values in Col_6 and store the result in col_7 for each row in the table, taking into account NULL values in either column (Col_5, Col_6).  We are adding numbers only. No data conversion. It could be a an update statement or Merge statement.

I tried this, but  did not work.

Update Alpha set Col_7 = ((Select Case when col_5 = -1 or col_6 = -1 then -1
else  col_5 + col_6
End)
From Alpha);

Please help.

Thanks
0
Question by:KamalAgnihotri
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
7 Comments

LVL 14

Expert Comment

ID: 41765501
update Alpha set .... From Alpha

I suppose you're getting an error?!
0

Author Comment

ID: 41765538
Alex, Yes I am getting an error. What is the solution.
0

LVL 35

Expert Comment

ID: 41765590
I would guess this is what you are looking for:
``````UPDATE alpha
SET    col_7 = CASE
WHEN col_5 = -1
OR col_6 = -1 THEN -1
ELSE col_5 + col_6
END;
``````
Not sure why you have a sub-query.
0

LVL 32

Accepted Solution

awking00 earned 2000 total points
ID: 41765760
Since you indicated col_5 or col_6 might contain nulls, you may want to modify johnsone's update to this
UPDATE alpha
SET    col_7 = CASE
WHEN col_5 = -1
OR col_6 = -1 THEN -1
ELSE nvl(col_5,0 + nvl(col_6,0)
END;
You might also use decode in this instance
update alpha set col_7 = decode(col_5,-1,-1,col_6,-1,-1, nvl(col_5,0) + nvl(col_6,0))
0

LVL 35

Expert Comment

ID: 41765814
I was assuming that the original was correct, just needed the syntax fixed.
0

LVL 38

Expert Comment

ID: 41765853
what ? really ?

update alpha
set col7 = nvl(col5, 0) + nvl(col6, 0)

i guess I spoiled your homework now
1

Author Closing Comment

ID: 41766674
Thanks. That worked.
0

## Featured Post

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
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.
###### Suggested Courses
Course of the Month8 days, 20 hours left to enroll

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

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