Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Conditional replace Function

7eb33dbf-5ba7-4248-8088-339e77b11c5eJan 20 2016 — edited Jan 20 2016

Good afternoon all,

I am trying to affect what i'm sure is a basic replace function on some of the values in the first column of a table dependent on the value of the second column. So if i run the simple SELECT shown below i get the following:

----SCRIPT----

SELECT Col_name_1, Col_name_2

FROM Table_Name

----Results----

Col_name_1     Col_name_2

     13                    0    

     23                    0

     44                    1

     47                    1

     50                    0

Now say i wanted to replace the values in 'Col_name_1' to negative values if the value in the same row of 'Col_name_2' is 1. Giving the following result.

Col_name_1     Col_name_2

     13                    0    

     23                    0

    -44                    1

    -47                    1

     50                    0


What would be the most efficient way to do this. I was thinking of using logic along the lines of if(Col_name_2=1,) then replace(col_name_1, '-&col_name_1') obviously the syntax of this is incorrect i was just wondering how i could produce a "conditional replace function" that will only affect certain rows of the  results table dependent on condition.

Many thanks

Charlie

This post has been answered by Frank Kulash on Jan 20 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 17 2016
Added on Jan 20 2016
9 comments
4,912 views