日期:2014-05-17 浏览次数:20588 次
create function sj_dis(@d varchar(50))
returns varchar(50)
begin
declare @tem float
set @tem=cast(@d as float)
if @tem between 10000 and 100000
set @tem=round(@tem/100,0)*100
if @tem between 1000 and 10000
set @tem=round(@tem/10,0)*10
if @tem between 100 and 1000
set @tem=@tem
if @tem between 10 and 100
set @tem=round(@tem,1)
return @tem
end
create function abc(@d float,@m int) -- @m=0舍入!=0截断
returns sql_variant
begin
declare @e int = log10(@d)
declare @v float = round(@d,2-@e,@m)
if @v>=100 return convert(int,@v)
if @v>=10 return convert(dec(3,1),@v)
return convert(dec(3,2),@v)
end
go
select dbo.abc(987654321,0) union all
select dbo.abc(987654321,1) union all
select dbo.abc(12345,0) union all
select dbo.abc(1234,0) union all
select dbo.abc(123.45,0) union all
select dbo.abc(12.345,0) union all
select dbo.abc(12.00,0) union all
select dbo.abc(1.2345,0) union all
select dbo.abc(99.99,0) union all
select dbo.abc(99.99,1)
/*
988000000
987000000
12300
1230
123
12.3
12.0
1.23
100
99.9
*/