拆分函数逗号分隔符SQL Server

时间:2015-04-05 00:39:08

标签: sql-server function split

我想将函数拆分为两个单独的列,并使用给定的输入字符串。

如果我执行此函数,我将获得行列表值

select * 
from dbo.Split ('sunday,9,monday,8,tuesday,9, wednesday 8, thursday 8,friday,9, saturday,8')

像行值一样输出

sunday 
9
monday
8
tuesday
9
wednesday
8 
thursday
8 
friday
9
saturday 
8

我的分割功能是:

create  function dbo.Split
(
    @String varchar(max)
)
returns @SplittedValues table
(
    Id varchar(50)  
)
as
begin
    declare @SplitLength int, @Delimiter varchar(5)

    set @Delimiter = ','

    while len(@String) > 0
    begin 
        select @SplitLength = (case charindex(@Delimiter,@String) when 0 then
            len(@String) else charindex(@Delimiter,@String) -1 end)

        insert into @SplittedValues
        select substring(@String,1,@SplitLength) 

        select @String = (case (len(@String) - @SplitLength) when 0 then  ''
            else right(@String, len(@String) - @SplitLength - 1) end)
    end 
return  
end

但是我需要输出两天的日期(第一天参数列表),小时,

+-----------+-------+
|   Days    | Hours |
+-----------+-------+
| Sunday    |    9  |
| Monday    |    8  |
| Tuesday   |    9  |
| Wednesday |    8  |
| Thursday  |    8  |
| Friday    |    9  |
| saturday  |    8  |
+-----------+-------+

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

@ user3442289您也可以使用以下代码。我刚刚修改了代码中的一些语句。

create  function dbo.Split2
(
    @String varchar(max)
)
returns @SplittedValues table
(
    Id varchar(50)  ,
    Id1 int
)

begin
    declare @SplitLength int, @Delimiter varchar(5)

    set @Delimiter = ','

    while len(@String) > 0
    begin 
        select @SplitLength = (case charindex(@Delimiter,@String) when 0 then
            len(@String) else charindex(@Delimiter,@String)  end)

        insert into @SplittedValues
        select substring(@String,1,@SplitLength-1) ,SUBSTRING(@string,@SplitLength+1,1)

                select @String = (case (len(@String) - @SplitLength) when 0 then  ''
            else right(@String, len(@String) - @SplitLength-1) end)

            if @string<>'' 
            begin
            set @string=(select substring(@string,2,len(@string)))
            end

    end 
    return
end

答案 1 :(得分:0)

可能有更漂亮的方法。

create  function dbo.TwoColumnSplit
(
    @String varchar(max)
)
returns @returnTable table
(
    LeftId varchar(50)
    , RightId varchar(50)  
)
as
begin

declare @numCols int = 2
declare @itr int = 0
declare @SplittedValues table
(
    rowID int,
    colID int,
    Id varchar(50)  

)
    declare @SplitLength int, @Delimiter varchar(5)

    set @Delimiter = ','

    while len(@String) > 0
    begin 
        select @SplitLength = (case charindex(@Delimiter,@String) when 0 then
            len(@String) else charindex(@Delimiter,@String) -1 end)

        insert into @SplittedValues(rowID, colID, Id)
        select @itr / @numCols, @itr % @numCols, substring(@String,1,@SplitLength) 

        select @String = (case (len(@String) - @SplitLength) when 0 then  ''
            else right(@String, len(@String) - @SplitLength - 1) end)

        set @itr = @itr + 1
    end 

insert into @returnTable(LeftId, RightId)
select l.id as LeftId, r.id as RightId
from
(select rowid, id from @SplittedValues where colid = 0) l
join
(select rowid, id from @SplittedValues where colid = 1) r
on l.rowid = r.rowID

return
END

go

select * from  [dbo].[TwoColumnSplit]('sunday,9,monday,8,tuesday,9, wednesday, 8, thursday, 8,friday,9, saturday,8')