有时我们需输出多个字段都需相除的字段值,且是带上百分号的,这时我们可以考虑自己写个方法来实现,那么要怎么写呢?
可以参考下面的方法来实现两数相除,输出带百分比的结果:
create function [dbo].[fungetrate]( @num1 int, @num2 int)
returns nvarchar(10)
as
begin
declare @res varchar(10)
select @res = case when @num1 is null or @num2 is null then null
when @num1 = 0 then '0%'
when @num2 = 0 then null
else concat(cast(cast(@num1 * 1.0 /@num2 as decimal(18,4)) * 100 as decimal(18,2)),'%') end
return @res
end
示例:
希望可以帮到你!