zoukankan      html  css  js  c++  java
  • 拆分字符串的自定义SQL函数,返回包含拆分后各子符的表

    ALTER FUNCTION [dbo].[func_StrSplit] (

     /* 待拆分的字符串  */

     @origStr varchar(7000),

     /* 拆分标记,如','  */

     @markStr varchar(100)

    )  

    RETURNS @splittable table        /*返回一个表*/

    (

     /* 编号ID */

     str_id   varchar(4000) NOT NULL,

     /* 拆分后的字符串  */

     string   varchar(2000) NOT NULL

     

    )

     /************************************************************

     *功能: 拆分字符串       *

     *************************************************************/

    AS  

     

    BEGIN

     declare @strlen int,@postion int,@start int,@sublen int,@TEMPstr varchar(200),@TEMPid int

     SELECT @strlen=LEN(@origStr),@start=1,@sublen=0,@postion=1,@TEMPstr='',@TEMPid=0

     

     if(RIGHT(@origStr,1)<>@markStr )

     begin

      set @origStr = @origStr + @markStr

     end

     WHILE((@postion<=@strlen) and (@postion !=0))

     BEGIN

      IF(CHARINDEX(@markStr,@origStr,@postion)!=0)

      BEGIN 

       SET @sublen=CHARINDEX(@markStr,@origStr,@postion)-@postion;  

      END

      ELSE

      BEGIN

       SET @sublen=@strlen-@postion+1;

     

      END

      IF(@postion<=@strlen)

      BEGIN

       SET @TEMPid=@TEMPid+1;

       SET @TEMPstr=SUBSTRING(@origStr,@postion,@sublen);

       INSERT INTO @splittable(str_id,string) values(@TEMPid,@TEMPstr)

       IF(CHARINDEX(@markStr,@origStr,@postion)!=0)

       BEGIN

        SET @postion=CHARINDEX(@markStr,@origStr,@postion)+1

       END

       ELSE

       BEGIN

        SET @postion=@postion+1

       END

      END 

     END

     RETURN 

    END

     

  • 相关阅读:
    设计模式——观察者模式
    LeetCode OJ——Word Ladder
    unorder_set<typename T> 学习
    LeetCode OJ——Text Justification
    LeetCode OJ——Two Sum
    LeetCode OJ——Longest Valid Parentheses
    路飞学城Python-Day16
    路飞学城Python-Day15
    路飞学城Python-Day14(practise)
    路飞学城Python-Day14
  • 原文地址:https://www.cnblogs.com/wiseant/p/1866086.html
Copyright © 2011-2022 走看看