代码改变世界

sql如何判断字符串从左边第一个数字为5

2011-09-22 22:24  starlet  阅读(1136)  评论(0编辑  收藏  举报
create function [dbo].[myfn_GetBeginFive](@str varchar(50)) 
returns bit
as
begin

 declare @r bit@index int@temp varchar(2),@count int
 set @r = 0
 set @index = 1 
 set @count=len(@str)
 set @temp = ''
 while @index < @count
 begin
  set @temp = substring(@str,@index,1)
  if '1234567890' like '%'+@temp+'%'
  begin
   if @temp='5'
   begin
     set @r=1
    end
    break
  end
  else
   set @index=@index+1
  end
 return @r
end

--执行函数,显示结果为 

--select '数字5开头' as 结果 where  dbo.[myfn_GetBeginFive]('HB5979uiu') = 1

GO