从数字中删除SQL Server中数字的最后一个零

时间:2017-09-29 06:18:41

标签: sql sql-server sql-server-2008

我在SQL Server中有一个numeric列,我的值如下所示:

 4.00
 6.25
 6.00
 6.80
10.00
12.00

我希望显示

4
6.25
6
6.8
10
12

提前感谢

4 个答案:

答案 0 :(得分:3)

您可以尝试:

select round(CONVERT (VARCHAR(50), 6.00,128),2)

答案 1 :(得分:1)

Numeric功能中''传递您的ROUND值,如下所示:

SELECT ROUND('4.00', 2)

SELECT ROUND('6.25', 2)

SELECT ROUND('6.00', 2)

SELECT ROUND('6.80', 2)

结果:

enter image description here

答案 2 :(得分:0)

如果列数据类型是数字(18,2),那么您可以创建自己的函数:

use db_test;
go

create function dbo.trunc_numeric (@value numeric(18, 2))
returns varchar(20)
as
begin
    declare @a bigint = (@value * 100) % 100;
    declare @b bigint = (@value * 100) % 10;
    if @a = 0
        return cast(cast(@value as numeric(18, 0)) as varchar(20));
    if @a <> 0 and @b = 0
        return cast(cast(@value as numeric(18, 1)) as varchar(20));
    return cast(@value as varchar(20));
end

select dbo.trunc_numeric(10.123);
select dbo.trunc_numeric(10.120);
select dbo.trunc_numeric(10.100);
select dbo.trunc_numeric(10.000);

答案 3 :(得分:0)

你可以这样做

SELECT  ROUND(CAST(col AS VARCHAR), 2) AS [Number]
FROM(
     SELECT 4.00 AS col
     UNION ALL
     SELECT 6.25 AS col
     UNION ALL
     SELECT 6.00 AS col
     UNION ALL
     SELECT 6.80 AS col
     UNION ALL
     SELECT 10.00 AS col
     UNION ALL
     SELECT 12.00 AS col
)x