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

    工欲善其事,必先利其器。
  • 相关阅读:
    LeetCode Algorithm
    实现QObject与JavaScript通讯(基于QWebEngine + QWebChannel)
    Linux工具参考篇(网摘)
    GO 语言简介(网摘)
    Qt窗体引用window自带阴影边框效果
    无插件VIM编程技巧(网摘)
    ASP.NET 5基础之中间件
    ASP.NET Core 1.0基础之应用启动
    理解ASP.NET 5 Web Apps
    DNX 概览
  • 原文地址:https://www.cnblogs.com/zhangzhu/p/2545269.html
Copyright © 2011-2022 走看看