SQL oracle中的正值负值和负值正值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20139423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:12:56  来源:igfitidea点击:

Positive value to negative and negative to positive in oracle

sqloracle

提问by Pravin Satav

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

我需要将正值转换为负值,将负值转换为正值,如果为 0,则保持原样。我可以在 sql 中做到这一点,只需要知道是否有更好的方法/替代方法来做到这一点?

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;

Database Oracle - 11g

数据库 Oracle - 11g

回答by Alen Oblak

What about multiplying with -1 ?

乘以 -1 怎么样?

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

回答by vhadalgi

how about just putting a negative sign

只加一个负号怎么样

SELECT - -15 as inverse;

-->15

SELECT  -15 as inverse;

-->-15

回答by Dalton Bohning

How about using subtraction:

如何使用减法:

SELECT 0 - a "changed value"
from test_tab;

回答by vasin1987

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

that's all you need

这就是你所需要的