1

How can I change all the values of a single column for other ones in one single order? For example, I want to change old values of the last column salary (2250,1,3500,1) for new ones (2352,7512,4253,1142). I have this database:

enter image description here

I know how to do it but changing step by step, and it is not efficient if I have a lot of rows. This way:

UPDATE TABLE tablename
   SET salary = REPLACE(tablename.salary, 2250, 2352);

and then perform that operation multiple times.

2
  • You need some logic on how you're going to change them if you want to change them all in one go without other data. If you have no logic then you either have to store what you want to update the salaries to in some other location or you need to perform individual updates. Commented Aug 10, 2014 at 14:57
  • Hi Ben, I am practicing with a simple table that has this columns: employeeID, name, surname, date, salary. Name is the primary key. So, it is not possible to change all at once? (I will try to show my simple table in a list format with StackOv text editor). Commented Aug 10, 2014 at 15:04

3 Answers 3

3
UPDATE TABLE tablename
SET salary = 2250
WHERE salary = 2352

I'm not sure what you're aiming for with the REPLACE() function but if you want to change the values then you need to do it like the above code. Set the salary to what you want WHERE it has a salary of 2250.

You can write it a few times with the different criteria and then run it.

EDIT: Since you're worried about doing this numerous times you can create a table called salaries:

CREATE TABLE t_salary AS 
SELECT salary from tablename;

ALTER t_salary add newsalary integer after salary;

In the 'newsalary' column you can add what the new salary should be then do an inner join. I just created a table for this purpose called stackoverflow (which would be your 'tablename'

update stackoverflow s
inner join t_salary ns on s.salary = ns.salary
set s.salary = ns.newsalary;

Now what this will do is join tablename to t_salary where the current salary = the salary in t_salary. Then you set the tablename.salary equal to the new salary, this worked for me I hope it works for you.

Note, the syntax may be slightly different since I don't have Oracle installed on my home machine, I used MySQL.

Sign up to request clarification or add additional context in comments.

6 Comments

@Jezzabeans but suppoose he has 1000 of rows to edit then he cant use your code beacause if he will use your code to update 1000 of rows at a time then he will need to use for loop and i think this option is very bad. Please reply if i am wrong?
Thanks for your answer Jezzabeanz. It is a simple form to do it, but do you know if there is any form to do it replacing the entire column that I type separated by commas for the one that is already saved? Thx in advance.
Yes Hardik you're right but only if there are 1000 different salaries. I'm looking around the web for some solutions but it doesn't seem possible, it really depends on how many distinct salaries you have. SELECT distinct salary from table name If you have more than 10 then you would certainly need a function or procedure, I'll get back to you when I find something.
Please see my updated answer with something that worked for me using a new table and an inner join.
@Jezzabeanz thanks for your time. This solution seems much more efficient. I'll give it a shot with my Oracle machine.
|
0

Since you already a list old salary values and their corresponding new salary values you can place them in a flat file and create an external table in oracle to point to this file.

Once that is done then you can just fire a simple update statement similar to the one given below:

update test1 set salary = ( select newsalary from test2 where test1.empid = test2.empid);

Comments

-1

UPDATE tablename SET salary = 2250 WHERE salary = 2352

This works fine for Oracle SQL.

2 Comments

"I know how to do it but changing step by step, and it is not efficient if I have a lot of rows." - how does your answer resolve this better?
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.