Positive value to negative and negative to positive in oracle

I have a requirement to convert the postive value to negative and negative value to positive, and if its 0, then leave it as it is. I can do it in sql, just need to know if there is a better way / alternative way to do this?

create table test_tab 
(a number);

insert into test_tab values (10);
insert into test_tab values (-10);
insert into test_tab values (0);
insert into test_tab values (10.15);
insert into test_tab values (-10.15);

select a , decode(sign(a),-1,abs(a),1,-abs(a),0) "changed value" from test_tab;

Oracle Database - 11g

+4
source share
4 answers

How about multiplying by -1?

select a
,      -1 * a "changed value"
from   test_tab;
+12
source

how to put a negative sign

SELECT - -15 as inverse;

-->15

SELECT  -15 as inverse;

-->-15
+3
source

:

SELECT 0 - a "changed value"
from test_tab;
+3
select a , decode(sign(a),-1,abs(a),1,-abs(a),0) -a from test_tab;

,

0

All Articles