格式编号n SQL

时间:2016-10-19 09:31:29

标签: sql sql-server

我想格式化这样的数字:

CentOS Linux release 7.2.1511 (Core)65803.12345

65803.1265803.98500

3 个答案:

答案 0 :(得分:1)

如果你想得到小数点后只有两位数,没有天花板或地板。

DECLARE @i AS float 
DECLARE @TmpInt AS INT 
DECLARE @TmpDec AS FLOAT 

SELECT @i = 65803.12345 
SELECT @TmpInt = (SELECT CAST(@i AS INT))
SELECT @TmpDec = @i - @TmpInt
SELECT CAST(((CAST(@TmpInt AS VARCHAR(50)) + SUBSTRING((LEFT((CAST(@TmpDec AS VARCHAR(50))), 4)), 2, 3))) AS DECIMAL(10, 2))
-- Output : 65803.12

SELECT @i = 65803.98500
SELECT @TmpInt = (SELECT CAST(@i AS INT))
SELECT @TmpDec = @i - @TmpInt
SELECT CAST(((CAST(@TmpInt AS VARCHAR(50)) + SUBSTRING((LEFT((CAST(@TmpDec AS VARCHAR(50))), 4)), 2, 3))) AS DECIMAL(10, 2))
-- Output : 65803.98

SELECT @i = 65803.12666
SELECT @TmpInt = (SELECT CAST(@i AS INT))
SELECT @TmpDec = @i - @TmpInt
SELECT CAST(((CAST(@TmpInt AS VARCHAR(50)) + SUBSTRING((LEFT((CAST(@TmpDec AS VARCHAR(50))), 4)), 2, 3))) AS DECIMAL(10, 2))
-- Output : 65803.12

答案 1 :(得分:0)

您可以使用ROUND(,)

ROUND(column, decimals)

更多信息:W3Schools detailed information

答案 2 :(得分:0)

试试这个

 declare @s decimal(12,5)
 set @s = 65803.98500
 select cast(@s-@s%1+cast(@s%1*100 as int)*0.01 as decimal(12,2))

结果是

65803.98