sql截取字符串

-- select Grab_SYS.dbo.SplitSubString('123#456','#',2) -- 456
ALTER function [dbo].[SplitSubString] 
( 
 @Expression varchar(8000) 
,@Delimiter varchar(100) 
,@ int 
) 
returns varchar(8000) 
as 
begin 

declare @p int 
set @p = CharIndex(@Delimiter,@Expression) 
if @p > 0
begin
   set @p = @p + len(@Delimiter) - 1
end
declare @i int 
set @i = 1 
while @i < @ 
begin  
   set @i = @i + 1
   set @Expression = substring (@Expression, @p + 1,len(@Expression) - @p ) 
   set @p = CharIndex(@Delimiter,@Expression)
   if @p > 0
   begin
      set @p = @p + len(@Delimiter) - 1
   end
   else
   begin
      break
   end
end 

declare @s varchar(1000) 
if @p = 0 and @i = @
begin 
   set @s = @Expression
end 
else if @i = @ 
begin 
   set @s = substring(@Expression, 1,@p - len(@Delimiter)) 
end 
return @s
end 

 

你可能感兴趣的:(数据库)