SQL常用自定函数 字符串处理 (转)

Create  FUNCTION [dbo].[getCheckParam]
   (
  @CheckParam varchar(100),   --参数
  @char varchar(1),     --分隔符号
  @index int            --第几个数  类似到数组,从0开始  当超过索引时取最后一个
   )
RETURNS varchar(100)
--分析 类似(类型|物品参数|数量)字符串组合,取出值
AS
BEGIN 
 declare @_index int 
 declare @i int set @i=0

 set @_index=charindex(@char,@CheckParam)
 while @_index>0
  begin    
   if @i=@index
   begin
    return substring(@checkParam,0,@_index)
   end
   set @i=@i+1
   set @_index=charindex(@char,@CheckParam)
   set @CheckParam=substring(@checkParam,@_index+1,len(@Checkparam))
  end
 return @CheckParam
 
END

实例:select dbo.getCheckParam('123|456|789','|',2)

取出 789

例似 C#中 '123|456|789'.split('|')[2]

原文地址:https://www.cnblogs.com/luluping/p/1989684.html