Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

postgresql

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?

like image 572
Jet Avatar asked Nov 29 '22 23:11

Jet


2 Answers

Try this:

 Update table set a= 0-a where a >0
like image 56
Muhammad Muazzam Avatar answered Dec 04 '22 06:12

Muhammad Muazzam


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;

like image 32
Nick Avatar answered Dec 04 '22 06:12

Nick