Usher
Usher

Reputation: 2146

update a column with condition thru Sql Query

I have column A B C D

I am trying to update Column C with divide functionality

e.g.

If Column C contains value 0.9 then i want to update that value in to 1/0.9= 1.33333 (round it to 1.3)

So the column has to update from 0.9 to 1.3.

Is there a way to do by SQL query with out store procedure?

Upvotes: 1

Views: 38

Answers (3)

Bohemian
Bohemian

Reputation: 424983

Since you haven't indicated which database you are using, I will offer a query that works for both mysql, postgres and sqlserver (that covers most database instances):

UPDATE mytable SET
columnC = CAST( 1 / columnC AS DECIMAL(8,1))
WHERE columnC = 0.9

Upvotes: 1

Kamil Gosciminski
Kamil Gosciminski

Reputation: 17137

Remember to cast your values to proper ones if they are not.

UPDATE tabl1e SET columnC = ROUND(1 / 0.9, 2) WHERE columnC = 0.9;

Also, 1/0.9 rounded to 2 decimal places gives 1,11

Upvotes: 1

Joe Taras
Joe Taras

Reputation: 15379

Try this:

UPDATE table SET columnC = 1 / columnC where columnC = 0.9

Upvotes: 1

Related Questions