Jet
Jet

Reputation: 3288

Convert positive data into negetive data (entire column) in Postgres Server

The current data in my table is:

  a   b
---------  
 -1   5
 -11  2   
 -5  32

My request is to convert every data of column a into negative value.

But how to update the positive values into the negative selecting the entire column?

Upvotes: 4

Views: 12089

Answers (3)

ezvine
ezvine

Reputation: 868

Just as @a_horse_with_no_name mentioned in his comments, the straightforward solution would be:

To negate all the existing values:

UPDATE mytable SET a = -a;

And if u want to make sure all values are negative:

UPDATE mytable SET a = -a where a > 0;

Upvotes: 0

Muhammad Muazzam
Muhammad Muazzam

Reputation: 2800

Try this:

 Update table set a= 0-a where a >0

Upvotes: 14

Nick
Nick

Reputation: 7431

UPDATE mytable SET a = a * -1;

This multiplies all values in 'a' by -1. Now, if the value is already negative, it will become positive. You you want to make sure they are always negative, do this:

UPDATE mytable SET a = a * -1 WHERE a > 0;

Upvotes: 8

Related Questions