SqlServer 2005 将字符串强制转换成float类型 20090317

今天写了一个数据库的函数
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


你可能感兴趣的:(Go)