• SQL分割字符串2


    create function [dbo].[SplitString]

    (
        @Input nvarchar(max),
        @Separator nvarchar(max)=',',
        @RemoveEmptyEntries bit=1 ,
        @position int
    )
    returns @TABLE table
    (
        [Id] int identity(1,1),
        [Value] nvarchar(max)
    )
    as
    begin
        declare @Index int, @Entry nvarchar(max)
        set @Index = charindex(@Separator,@Input)
        set @Input = @Input + @Separator
        
        declare @count int
        set @count = 1
        while (@Index>0)
        begin
            set @Entry=ltrim(rtrim(substring(@Input, 1, @Index-1)))
            
            if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry<>'')
                begin
                  if @count = @position
                    begin
                       insert into @TABLE([Value]) Values(@Entry)
                    end
                end

            set @Input = substring(@Input, @Index+datalength(@Separator)/2, len(@Input))
            set @Index = charindex(@Separator, @Input)
            set @count =  @count + 1;
        end
       
        return
    end

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

         select  [Value] from [dbo].[SplitString2]('2012-12-25', '-', 1,1)
         select  [Value] from [dbo].[SplitString2]('2012-12-25', '-', 1,2)
         select  [Value] from [dbo].[SplitString2]('2012-12-25', '-', 1,3) 

    工欲善其事,必先利其器。
  • 相关阅读:
    route-over VS mesh-under
    IOS算法(三)之插入排序
    GitHub学习笔记
    Python-面向对象 (二 继承)
    POJ 3518 Prime Gap(素数题)
    struts2的总体回想(ACTION、拦截器、值栈、OGNL表达式、ModelDriven方案等)
    first move advantage_百度搜索
    【绿茶书情】:《SOHO小报》和《凤…
    潘石屹的SOHO小报猝死
    ASP.NET Hashtable输出JSON格式数据
  • 原文地址:https://www.cnblogs.com/zhangzhu/p/2547360.html
Copyright © 2020-2023  润新知