今天写了一个数据库的函数
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
create function [dbo].[cc2sf_convert]( @my_str varchar(100))
returns varchar(100)
begin
declare @length int
declare @uni_int int
declare @i int
set @i = 0
set @length = len(@my_str)
while (@i <@length)
BEGIN
set @uni_int = unicode(substring(@my_str,@i+1,1));
IF @uni_int = unicode('.') or(48<= @uni_int and @uni_int<=57)
set @i = @i +1;
ELSE
break;
END
return substring(@my_str,1,@i)
end
调用范例:
dbo.cc2sf_convert('20.1万元');
------
result: 20.1
所有的函数快都是由
begin
end
构成他们替代了 { }
例如上面的函数同样可以写为:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER function [dbo].[myconvert] ( @my_str varchar(100)) returns varchar(100)
begin
declare @length int
declare @uni_int int
declare @i int
set @i = 0
set @length = len(@my_str)
while (@i <@length)
BEGIN
set @uni_int = unicode(substring(@my_str,@i,@i+1));
IF @uni_int = unicode('.') or(48<= @uni_int and @uni_int<=57)
begin
set @i = @i +1;
end
ELSE
begin
break;
end
END
return substring(@my_str,1,@i)
end
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
create function [dbo].[cc2sf_convert]( @my_str varchar(100))
returns varchar(100)
begin
declare @length int
declare @uni_int int
declare @i int
set @i = 0
set @length = len(@my_str)
while (@i <@length)
BEGIN
set @uni_int = unicode(substring(@my_str,@i+1,1));
IF @uni_int = unicode('.') or(48<= @uni_int and @uni_int<=57)
set @i = @i +1;
ELSE
break;
END
return substring(@my_str,1,@i)
end
调用范例:
dbo.cc2sf_convert('20.1万元');
------
result: 20.1
所有的函数快都是由
begin
end
构成他们替代了 { }
例如上面的函数同样可以写为:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER function [dbo].[myconvert] ( @my_str varchar(100)) returns varchar(100)
begin
declare @length int
declare @uni_int int
declare @i int
set @i = 0
set @length = len(@my_str)
while (@i <@length)
BEGIN
set @uni_int = unicode(substring(@my_str,@i,@i+1));
IF @uni_int = unicode('.') or(48<= @uni_int and @uni_int<=57)
begin
set @i = @i +1;
end
ELSE
begin
break;
end
END
return substring(@my_str,1,@i)
end
本文介绍了一个SQL函数,用于从输入的字符串中提取数值部分。该函数通过检查每个字符的Unicode值来判断是否为数字或小数点,并进行相应处理。
3425

被折叠的 条评论
为什么被折叠?



