zoukankan      html  css  js  c++  java
  • SQLserver字符串分割函数

     

    一、按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果。
    SQLserver字符串分割函数
    SQLserver字符串分割函数CREATE function Get_StrArrayLength
    SQLserver字符串分割函数(
    SQLserver字符串分割函数  @str varchar(1024),  --要分割的字符串
    SQLserver字符串分割函数  @split varchar(10)  --分隔符号
    SQLserver字符串分割函数)
    SQLserver字符串分割函数returns int
    SQLserver字符串分割函数as
    SQLserver字符串分割函数begin
    SQLserver字符串分割函数  declare @location int
    SQLserver字符串分割函数  declare @start int
    SQLserver字符串分割函数  declare @length int
    SQLserver字符串分割函数
    SQLserver字符串分割函数  set @str=ltrim(rtrim(@str))
    SQLserver字符串分割函数  set @location=charindex(@split,@str)
    SQLserver字符串分割函数  set @length=1
    SQLserver字符串分割函数  while @location<>0
    SQLserver字符串分割函数  begin
    SQLserver字符串分割函数    set @start=@location+1
    SQLserver字符串分割函数    set @location=charindex(@split,@str,@start)
    SQLserver字符串分割函数    set @length=@length+1
    SQLserver字符串分割函数  end
    SQLserver字符串分割函数  return @length
    SQLserver字符串分割函数end
    SQLserver字符串分割函数调用示例:select dbo.Get_StrArrayLength('78,1,2,3',',')
    SQLserver字符串分割函数返回值:4
    SQLserver字符串分割函数
    SQLserver字符串分割函数二、按指定符号分割字符串,返回分割后指定索引的第几个元素,象数组一样方便
    SQLserver字符串分割函数
    SQLserver字符串分割函数CREATE function Get_StrArrayStrOfIndex
    SQLserver字符串分割函数(
    SQLserver字符串分割函数  @str varchar(1024),  --要分割的字符串
    SQLserver字符串分割函数  @split varchar(10),  --分隔符号
    SQLserver字符串分割函数  @index int --取第几个元素
    SQLserver字符串分割函数)
    SQLserver字符串分割函数returns varchar(1024)
    SQLserver字符串分割函数as
    SQLserver字符串分割函数begin
    SQLserver字符串分割函数  declare @location int
    SQLserver字符串分割函数  declare @start int
    SQLserver字符串分割函数  declare @next int
    SQLserver字符串分割函数  declare @seed int
    SQLserver字符串分割函数
    SQLserver字符串分割函数  set @str=ltrim(rtrim(@str))
    SQLserver字符串分割函数  set @start=1
    SQLserver字符串分割函数  set @next=1
    SQLserver字符串分割函数  set @seed=len(@split)
    SQLserver字符串分割函数  
    SQLserver字符串分割函数  set @location=charindex(@split,@str)
    SQLserver字符串分割函数  while @location<>0 and @index>@next
    SQLserver字符串分割函数  begin
    SQLserver字符串分割函数    set @start=@location+@seed
    SQLserver字符串分割函数    set @location=charindex(@split,@str,@start)
    SQLserver字符串分割函数    set @next=@next+1
    SQLserver字符串分割函数  end
    SQLserver字符串分割函数  if @location =0 select @location =len(@str)+1
    SQLserver字符串分割函数 --这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号。
    SQLserver字符串分割函数  
    SQLserver字符串分割函数  return substring(@str,@start,@location-@start)
    SQLserver字符串分割函数end
    SQLserver字符串分割函数调用示例:select dbo.Get_StrArrayStrOfIndex('8,9,4',',',2)
    SQLserver字符串分割函数返回值:9
    SQLserver字符串分割函数
    SQLserver字符串分割函数三、结合上边两个函数,象数组一样遍历字符串中的元素
    SQLserver字符串分割函数
    SQLserver字符串分割函数create   function   f_splitstr(@SourceSql   varchar(8000),@StrSeprate   varchar(100))   
    SQLserver字符串分割函数  returns   @temp   table(F1   varchar(100))   
    SQLserver字符串分割函数  as     
    SQLserver字符串分割函数  begin   
    SQLserver字符串分割函数  declare   @ch   as   varchar(100)   
    SQLserver字符串分割函数  set   @SourceSql=@SourceSql+@StrSeprate     
    SQLserver字符串分割函数  while(@SourceSql<>'')   
    SQLserver字符串分割函数                  begin   
    SQLserver字符串分割函数                  set   @ch=left(@SourceSql,charindex(',',@SourceSql,1)-1)   
    SQLserver字符串分割函数  insert   @temp   values(@ch)   
    SQLserver字符串分割函数  set   @SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'')   
    SQLserver字符串分割函数                  end   
    SQLserver字符串分割函数  return   
    SQLserver字符串分割函数  end   
    SQLserver字符串分割函数    
    SQLserver字符串分割函数
    SQLserver字符串分割函数----调用
    SQLserver字符串分割函数  select   *   from   dbo.f_splitstr('1,2,3,4',',') 
    SQLserver字符串分割函数--结果:
    SQLserver字符串分割函数1
    SQLserver字符串分割函数2
    SQLserver字符串分割函数3
    SQLserver字符串分割函数4
  • 相关阅读:
    leetcode821
    leetcode872
    leetcode700
    leetcode806
    2019-9-2-win10-uwp-右击浮出窗在点击位置
    2019-9-2-win10-uwp-打包第三方字体到应用
    2019-10-18-dotnet-文件读写务必注意事项
    2018-8-10-win10-uwp-如何创建修改保存位图
    2018-8-9-win10-uwp-装机必备应用-含源代码
    2019-11-1-asp-dotnet-core-简单开发P2P中央服务器
  • 原文地址:https://www.cnblogs.com/lbliubinlb/p/4741400.html
Copyright © 2011-2022 走看看