zoukankan      html  css  js  c++  java
  • 【转】sql里面的split

    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)

    while (@Index>0)
    begin
    set @Entry=ltrim(rtrim(substring(@Input, 1, @Index-1)))

    if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry<>'')
    begin
    insert into @TABLE([Value]) Values(@Entry)
    end

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

    set @Entry=ltrim(rtrim(@Input))
    if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry<>'')
    begin
    insert into @TABLE([Value]) Values(@Entry)
    end

    return
    END


    declare @str1 varchar(max), @str2 varchar(max), @str3 varchar(max)

    set @str1 = '1,2,3'
    set @str2 = '1###2###3'
    set @str3 = '1###2###3###'

    select [Value] from [dbo].[SplitString](@str1, ',', 1)
    select [Value] from [dbo].[SplitString](@str2, '###', 1)
    select [Value] from [dbo].[SplitString](@str3, '###', 0)

  • 相关阅读:
    P2351 [SDOI2012]吊灯
    洛谷P1450 [HAOI2008]硬币购物 背包+容斥
    P5110 块速递推-光速幂、斐波那契数列通项
    AT2304 Cleaning
    CSP-S 2020
    CF487E Tourists
    P4334 [COI2007] Policija
    动态逆序对专练
    CF437D The Child and Zoo
    CF1032G Chattering
  • 原文地址:https://www.cnblogs.com/muer/p/sql_split.html
Copyright © 2011-2022 走看看