在 SQL Server 中将负数据转换为正数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9773909/
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
Convert negative data into positive data in SQL Server
提问by What'sUP
The current data of the table is:
该表的当前数据为:
a b
---------
-1 5
-11 2
-5 32
My request is to convert every data into positive value.
我的要求是将每个数据转换为正值。
Unfortunately, I forgot the name of the build-in function of SQL Server that enable to convert.
不幸的是,我忘记了启用转换的 SQL Server 内置函数的名称。
回答by Lamak
You are thinking in the function ABS
, that gives you the absolute value of a numeric data.
您正在考虑函数ABS
,它为您提供数字数据的绝对值。
SELECT ABS(a) AS AbsoluteA, ABS(b) AS AbsoluteB
FROM YourTable
回答by Abdul Saboor
The best Solution is:from Positive to negative or from negative to positive
最好的解决方案是:从正面到负面或从负面到正面
For negative:
对于否定:
SELECT ABS(a) * -1 AS AbsoluteA, ABS(b) * -1 AS AbsoluteB
FROM YourTable
For positive:
对于阳性:
SELECT ABS(a) AS AbsoluteA, ABS(b) AS AbsoluteB
FROM YourTable
回答by Sam DeHaan
UPDATE mytbl
SET a = ABS(a)
where a < 0
回答by Adam Porad
回答by ruddy simonpour
Easy and straightforward solution using CASEfunction
使用CASE函数的简单直接的解决方案
SELECT CASE WHEN ( a > 0 ) THEN (a*-1) ELSE (a*-1) END AS NegativeA,
CASE WHEN ( b > 0 ) THEN (b*-1) ELSE (b*-1) END AS PositiveB
FROM YourTableName