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)