分隔符分解字符串

declare @str nvarchar(50);
set @str='462,464,2';
select @str as '字符串'
select len(@str) as '字符长度'
select charindex(',',@str,1) as '第一个逗号的索引值'
select LEFT(@str,charindex(',',@str,1)-1) as '第一个值'
select SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)) as '从第一逗号开始截取出后面的字符串'
select LEFT(SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),CHARINDEX(',',SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),1)-1) as '中间的值'
select SUBSTRING(SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),charindex(',',SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),1)+1,len(@str)) as '最后面的值' --从第二个逗号开始截取出其后的字符串

===========================================================================

如果有一个字符串 eg: "sun,star,moon,clouds",想要在MS SQL中根据给定的分隔符','把这个字符串分解成各个元素[sun] [star] [moon] [clouds],如何实现呢?为此,创建一个Function,代码如下:

复制代码
CREATE  FUNCTION  [ dbo ]. [ Split_StrByDelimiter ]( @String  VARCHAR( 8000),  @Delimiter  CHAR( 1))       
   RETURNS  @temptable  TABLE (items  VARCHAR( 8000))       
AS       
     BEGIN       
      DECLARE  @idx  INT       
     DECLARE  @slice  VARCHAR( 8000)       
 
     SELECT  @idx  =  1       
          IF  len( @String) < 1  OR  @String  IS  NULL   RETURN       
 
       while  @idx !=  0       
        BEGIN       
          SET  @idx  =  charindex( @Delimiter, @String)       
          IF  @idx != 0       
              SET  @slice  =  LEFT( @String, @idx  -  1)       
         ELSE       
              SET  @slice  =  @String       
 
          IF( len( @slice) > 0)  
             INSERT  INTO  @temptable(Items)  VALUES( @slice)       
 
          SET  @String  =  RIGHT( @String, len( @String-  @idx)       
           IF  len( @String=  0  break       
      END   
    RETURN       
  END
复制代码

示例:如果输入 

SELECT  *  FROM dbo.Split_StrByDelimiter( ' sun,star,moon,clouds ', ' , ')

结果返回

sun

star

moon

clouds

 

在上面的代码做变形,返回有多少个元素

复制代码
CREATE  FUNCTION  [ dbo ]. [ GetCount_Split_StrByDelimiter ]( @String  VARCHAR( 8000),  @Delimiter  CHAR( 1))     
   RETURNS  INT  
AS  
   BEGIN      
    DECLARE  @temptable  TABLE (items  VARCHAR( 8000))   
    DECLARE  @SplitCount  INT
      DECLARE  @idx  INT       
     DECLARE  @slice  VARCHAR( 8000)       
 
     SELECT  @idx  =  1       
          IF  len( @String) < 1  OR  @String  IS  NULL   RETURN   0   
 
       while  @idx !=  0       
        BEGIN       
          SET  @idx  =  charindex( @Delimiter, @String)       
          IF  @idx != 0       
              SET  @slice  =  LEFT( @String, @idx  -  1)       
         ELSE       
              SET  @slice  =  @String       
 
          IF( len( @slice) > 0)  
             INSERT  INTO  @temptable(Items)  VALUES( @slice)       
 
          SET  @String  =  RIGHT( @String, len( @String-  @idx)       
           IF  len( @String=  0  break       
      END   
      SET   @SplitCount =( SELECT  COUNT( *FROM   @temptable)
    RETURN   @SplitCount
  END
复制代码

 示例

SELECT  dbo.GetCount_Split_StrByDelimiter( ' sun,star,moon,clouds ', ' , ')

结果返回

4

==========================================================================================



-- 獲取被分隔符分割開來的字符串中參數的個數

CREATE   function  GetStrArrayLength
(
 
@str   varchar ( 1024 ),   -- 要分割的字符串
  @split   varchar ( 10 )   -- 分隔符号
)
returns   int
as
begin
 
declare   @location   int
 
declare   @start   int
 
declare   @length   int

 
set   @str = ltrim ( rtrim ( @str )) -- 剔除字符串的首尾空格

 
set   @location = charindex ( @split , @str ) -- 第一個分隔符在字符串中的索引數字  例如'a1,b2,c3,d4'中第一個','的位置索引

 
set   @length = 1 -- 字符串被分隔符分割開來的參數個數,這裡初始化為一個

 
while   @location <> 0   -- 如果字符串中還存在分隔符那麼該分隔符在字符串中的索引就不會為0,也就是不等於0
  begin
   
set   @start = @location + 1   -- 從第一個分隔符索引位置後的字符串中開始檢索,檢索的起始是第一個分隔符後的一位開始
    set   @location = charindex ( @split , @str , @start )
   
set   @length = @length + 1
 
end
 
return   @length
end
-- -------------------------------------

-- 获得带分隔符的字符串中指定索引对应的间隔项
creat  FUNCTION  dbo.f_GetStr(
    
@s   varchar ( 8000 ),       -- 包含多个数据项的字符串
     @pos   int ,              -- 要获取的数据项的位置
     @split   varchar ( 10 )      -- 数据分隔符
) RETURNS   varchar ( 1000 )
AS
BEGIN
    
IF   @s   IS   NULL   RETURN ( NULL )
    
DECLARE   @splitlen   int
    
SELECT   @splitlen = LEN ( @split + ' a ' ) - 2
    
WHILE   @pos > 1   AND   CHARINDEX ( @split , @s + @split ) > 0
        
SELECT   @pos = @pos - 1 ,
            
@s = STUFF ( @s , 1 , CHARINDEX ( @split , @s + @split ) + @splitlen , '' )
    
RETURN ( ISNULL ( LEFT ( @s , CHARINDEX ( @split , @s + @split ) - 1 ), '' ))
END

 

-- ------------------------------------------------------------

获得指定索引对应的间隔开来的单项字符数据

create   function  GetStr
(
 
@str   varchar ( 1024 ),   -- 要分割的字符串
  @split   varchar ( 10 ),   -- 分隔符号
  @index   int   -- 取第几个元素
)
returns   varchar ( 1024 )
as
begin
 
declare   @location   int
 
declare   @start   int
 
declare   @next   int
 
declare   @seed   int

 
set   @str = ltrim ( rtrim ( @str ))
 
set   @start = 1
 
set   @next = 1
 
set   @seed = len ( @split )
 
 
set   @location = charindex ( @split , @str )
 
while   @location <> 0   and   @index > @next
 
begin
   
set   @start = @location + @seed
   
set   @location = charindex ( @split , @str , @start )
   
set   @next = @next + 1
 
end
 
if   @location   = 0   select   @location   = len ( @str ) + 1   
 
return   substring ( @str , @start , @location - @start )
end


 
复制代码

 



-- 獲取被分隔符分割開來的字符串中參數的個數

CREATE   function  GetStrArrayLength
(
 
@str   varchar ( 1024 ),   -- 要分割的字符串
  @split   varchar ( 10 )   -- 分隔符号
)
returns   int
as
begin
 
declare   @location   int
 
declare   @start   int
 
declare   @length   int

 
set   @str = ltrim ( rtrim ( @str )) -- 剔除字符串的首尾空格

 
set   @location = charindex ( @split , @str ) -- 第一個分隔符在字符串中的索引數字  例如'a1,b2,c3,d4'中第一個','的位置索引

 
set   @length = 1 -- 字符串被分隔符分割開來的參數個數,這裡初始化為一個

 
while   @location <> 0   -- 如果字符串中還存在分隔符那麼該分隔符在字符串中的索引就不會為0,也就是不等於0
  begin
   
set   @start = @location + 1   -- 從第一個分隔符索引位置後的字符串中開始檢索,檢索的起始是第一個分隔符後的一位開始
    set   @location = charindex ( @split , @str , @start )
   
set   @length = @length + 1
 
end
 
return   @length
end
-- -------------------------------------

-- 获得带分隔符的字符串中指定索引对应的间隔项
creat  FUNCTION  dbo.f_GetStr(
    
@s   varchar ( 8000 ),       -- 包含多个数据项的字符串
     @pos   int ,              -- 要获取的数据项的位置
     @split   varchar ( 10 )      -- 数据分隔符
) RETURNS   varchar ( 1000 )
AS
BEGIN
    
IF   @s   IS   NULL   RETURN ( NULL )
    
DECLARE   @splitlen   int
    
SELECT   @splitlen = LEN ( @split + ' a ' ) - 2
    
WHILE   @pos > 1   AND   CHARINDEX ( @split , @s + @split ) > 0
        
SELECT   @pos = @pos - 1 ,
            
@s = STUFF ( @s , 1 , CHARINDEX ( @split , @s + @split ) + @splitlen , '' )
    
RETURN ( ISNULL ( LEFT ( @s , CHARINDEX ( @split , @s + @split ) - 1 ), '' ))
END

 

-- ------------------------------------------------------------

获得指定索引对应的间隔开来的单项字符数据

create   function  GetStr
(
 
@str   varchar ( 1024 ),   -- 要分割的字符串
  @split   varchar ( 10 ),   -- 分隔符号
  @index   int   -- 取第几个元素
)
returns   varchar ( 1024 )
as
begin
 
declare   @location   int
 
declare   @start   int
 
declare   @next   int
 
declare   @seed   int

 
set   @str = ltrim ( rtrim ( @str ))
 
set   @start = 1
 
set   @next = 1
 
set   @seed = len ( @split )
 
 
set   @location = charindex ( @split , @str )
 
while   @location <> 0   and   @index > @next
 
begin
   
set   @start = @location + @seed
   
set   @location = charindex ( @split , @str , @start )
   
set   @next = @next + 1
 
end
 
if   @location   = 0   select   @location   = len ( @str ) + 1   
 
return   substring ( @str , @start , @location - @start )
end


 
复制代码

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值