sql奇进偶舍函数
create function sslr(@num numeric(20,10),@i int)
--@i保留的小数位
--@num传入的数据
returns varchar(20)
as
begin
declare @numtemp numeric(20,10),@result varchar(20)
set @numtemp=abs(@num)*power(10,@i+1)
select @numtemp=(case when floor(@numtemp)-floor(@numtemp/10)*10=5
then (case when @numtemp-floor(@numtemp)=0
then (case when cast(floor(@numtemp/10) as int)%2=0
then floor(@numtemp/10)/power(10,@i)
else round(@numtemp/power(10,@i+1),@i)
end)
else round(@numtemp/power(10,@i+1),@i)
end)
else round(@numtemp/power(10,@i+1),@i)
end)
set @result=(case when @num> 0 then str(@numtemp,20,@i) else str(0-@numtemp,20,@i) end)
return @result
end
--@i保留的小数位
--@num传入的数据
returns varchar(20)
as
begin
declare @numtemp numeric(20,10),@result varchar(20)
set @numtemp=abs(@num)*power(10,@i+1)
select @numtemp=(case when floor(@numtemp)-floor(@numtemp/10)*10=5
then (case when @numtemp-floor(@numtemp)=0
then (case when cast(floor(@numtemp/10) as int)%2=0
then floor(@numtemp/10)/power(10,@i)
else round(@numtemp/power(10,@i+1),@i)
end)
else round(@numtemp/power(10,@i+1),@i)
end)
else round(@numtemp/power(10,@i+1),@i)
end)
set @result=(case when @num> 0 then str(@numtemp,20,@i) else str(0-@numtemp,20,@i) end)
return @result
end
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步