执行sql中的 函数,function

CREATE FUNCTION [dbo].[GetWeight]
(
@Value decimal(18,3)--重量/体积重
)
returns decimal(18,1) 
as
begin 
--顺丰重量/体积重以0.5kg为单位向上取值(小数点后两位4舍5入)
declare @Weight decimal(18,3)
select @Weight=
             case when @Value<=1    then 1 
                  when @Value>1 and @Value%1>=0 and @Value%1<0.05 then @Value
                  when @Value>1 and @Value%1>=0.05 and @Value%1<0.55 then FLOOR(@Value)+0.5
                  when @Value>1 and @Value%1>=0.55 then FLOOR(@Value)+1
            end
return @Weight
end
GO

--floor(value)函数返回小于或等于指定值(value)的最小整数
select FLOOR('10.2')--10   向下取整
select FLOOR('10.6')--10
--ceiling(value)函数返回大于或等于指定值(value)的最小整数
select ceiling('10.2')--11  向上取整
select ceiling('10.6')--11
--round()函数遵循四舍五入原则,用于把数值字段舍入为指定的小数位数
 

DECLARE @Result decimal(18,3)
EXEC @Result= GetWeight '1.09958'
PRINT '函数的结果是:'+CONVERT(varchar(20),@Result)

 

posted @ 2019-08-17 15:58  ProZkb  阅读(4186)  评论(0编辑  收藏  举报