zoukankan      html  css  js  c++  java
  • SQL字符串分割


    Create function [dbo].[SplitString]
    (
        @Input nvarchar(max),
        @Separator nvarchar(max)=',',
        @RemoveEmptyEntries bit=1
    )
    returns @TABLE table
    (
        [Id] int identity(1,1),
        [Value] nvarchar(max)
    )
    as
    begin
        declare @Index int, @Entry nvarchar(max)
        set @Index = charindex(@Separator,@Input)
        
        declare @count int
        set @count = 0
        while (@Index>0)
        begin
            set @Entry=ltrim(rtrim(substring(@Input, 1, @Index-1)))
            
            if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry<>'')
                begin
                  if @count=1
                    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
        
        --set @Entry=ltrim(rtrim(@Input))
        --if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry<>'')
        --    begin
        --        insert into @TABLE([Value]) Values(@Entry)
        --    end

        return
    end 

     --只打印12
      declare  @years  nchar(10);
      select @years = [Value] from [dbo].[SplitString2]('2012-12-45', '-', 1);
      print @years

    工欲善其事,必先利其器。
  • 相关阅读:
    ThinkPHP3.2.3使用分页
    使用phpmailer发送邮件
    字体笔记
    jquery实现上传文件大小类型的验证
    两个矩阵中的dp题的差异
    Linux进程间通信——使用共享内存
    使用管道完成进程间通信(匿名管道pipe、命名管道fifo)
    为什么 C++ 有指针了还要引用?
    实现一个Memcpy函数
    猜帽子颜色问题
  • 原文地址:https://www.cnblogs.com/zhangzhu/p/2545269.html
Copyright © 2011-2022 走看看