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)

  • 相关阅读:
    SQL中char,varchar,nvarchar等的异同
    SQL中group by的用法
    如何管理自己的时间
    ref和out
    SQL中的日期时间函数
    SQL中的类型转换
    SQL中的自定义函数Function
    SQL中的模糊查询
    Struct是干什么的
    把普通图片转换成二进制
  • 原文地址:https://www.cnblogs.com/muer/p/sql_split.html
Copyright © 2011-2022 走看看