SqlServer经典函数之数字去零
时间:2021-07-01 10:21:17
帮助过:2人阅读
--去零函数 ---去除小数点后多余的零 select dbo.ClearZero(‘1245.3400‘) ---结果为1245.34
create function [dbo].
[ClearZero]
(@Number varchar(
200))
returns varchar(
200)
as
begin
if @Number = ‘‘ or @Number is null ---如果为空,直接返回
return null
else
begin
declare @PointIndex int
set @PointIndex = charindex(
‘.‘,
@Number)
if @PointIndex = 0 return @Number ---如果不是浮点数,直接返回
if @PointIndex = len(
@Number)
return replace(
@Number,
‘.‘,
‘‘)
----数字末尾有小数点,直接删除返回
else
begin
if right(
@Number,
1)
= ‘0‘ ---递归调用,进行去零操作
begin
set @Number = substring(
@Number,
1,
len(
@Number)
- 1)
return dbo.ClearZero(
@Number)
end
else
return @Number
end
end
return null
end
GO
SqlServer经典函数之数字去零
标签: