在循环

时间:2017-07-07 13:19:38

标签: sql-server split nvarchar

我有这样的nvarchar数据:

'20030,20031,20033,20034,20065,20045,20044'

我希望通过拆分数组或在拆分过程中逐一阅读来读取所有这些内容并进行进一步处理。

我试过这个good article但是没能做到。

4 个答案:

答案 0 :(得分:3)

DECLARE @valueList varchar(8000)
DECLARE @pos INT
DECLARE @len INT
DECLARE @value varchar(8000)

SET @valueList = '20030,20031,20033,20034,20065,20045,20044,' -- added delimiter to end of string

set @pos = 0
set @len = 0

WHILE CHARINDEX(',', @valueList, @pos+1)>0
BEGIN
    set @len = CHARINDEX(',', @valueList, @pos+1) - @pos
    set @value = SUBSTRING(@valueList, @pos, @len)

    PRINT(@value)   
    -- write your logic here
    set @pos = CHARINDEX(',', @valueList, @pos+@len) +1
END

答案 1 :(得分:2)

尝试使用此article使用函数来读取逗号分隔值并返回表

CREATE FUNCTION dbo.Split(@String nvarchar(4000), @Delimiter char(1))
RETURNS @Results TABLE (Items nvarchar(4000))
AS
BEGIN
DECLARE @INDEX INT
DECLARE @SLICE nvarchar(4000)
-- HAVE TO SET TO 1 SO IT DOESNT EQUAL Z
--     ERO FIRST TIME IN LOOP
SELECT @INDEX = 1
WHILE @INDEX !=0
BEGIN
-- GET THE INDEX OF THE FIRST OCCURENCE OF THE SPLIT CHARACTER
SELECT @INDEX = CHARINDEX(@Delimiter,@STRING)
-- NOW PUSH EVERYTHING TO THE LEFT OF IT INTO THE SLICE VARIABLE
IF @INDEX !=0
SELECT @SLICE = LEFT(@STRING,@INDEX - 1)
ELSE
SELECT @SLICE = @STRING
-- PUT THE ITEM INTO THE RESULTS SET
INSERT INTO @Results(Items) VALUES(@SLICE)
-- CHOP THE ITEM REMOVED OFF THE MAIN STRING
SELECT @STRING = RIGHT(@STRING,LEN(@STRING) - @INDEX)
-- BREAK OUT IF WE ARE DONE
IF LEN(@STRING) = 0 BREAK
END
RETURN
END

只需从存储过程调用函数或只调用如下函数:

SELECT items FROM [dbo].[Split] ('20030,20031,20033,20034,20065,20045,20044', ',') 

答案 2 :(得分:0)

一点也不难。使用递归

可以最好地解决它
create table #Testdata2(Data varchar(max))
insert #Testdata2 select '20030,20031,20033,20034,20065,20045,20044'



;with tmp(DataItem, Data) as (
select LEFT(Data, CHARINDEX(',',Data+',')-1),
    STUFF(Data, 1, CHARINDEX(',',Data+','), '')
from #Testdata2
union all
select LEFT(Data, CHARINDEX(',',Data+',')-1),
    STUFF(Data, 1, CHARINDEX(',',Data+','), '')
from tmp
where Data > ''
)
select DataItem
from tmp
OPTION (maxrecursion 0)

输出:

(select)
 Data
 ----------------
 20030,20031,20033,20034,20065,20045,20044


(query)
DataItem
----------------
20030
20031
20033
20034
20065
20045
20044

答案 3 :(得分:0)

使用String Split和Xml的另一种方法

DECLARE @string nvarchar(max)='20030,20031,20033,20034,20065,20045,20044'
DECLARE @Table AS TABLE (String nvarchar(max))
INSERT INTO @Table
SELECT @string

SELECT Row_NUmber ()OVER(ORDER BY (SELECT 1) )AS Seq, Split.a.value('.', 'VARCHAR(1000)') AS String
            FROM  (
                SELECT   CAST('<S>' + REPLACE(String, ',', '</S><S>') + '</S>' AS XML) AS String
                FROM @Table
                ) AS A
            CROSS APPLY String.nodes('/S') AS Split(a) 

上述代码可以合并到函数中以返回拆分字符串

CREATE FUNCTION dbo.udf_Split (
    @String NVARCHAR(4000)
    ,@Delimiter CHAR(1)
    )
RETURNS @Results TABLE (Items NVARCHAR(4000))
AS
BEGIN
    DECLARE @Table AS TABLE (String NVARCHAR(max))

    INSERT INTO @Table
    SELECT @String

    INSERT INTO @Results
    SELECT Split.a.value('.', 'VARCHAR(1000)') AS String
    FROM (
        SELECT CAST('<S>' + REPLACE(String, @Delimiter, '</S><S>') + '</S>' AS XML) AS String
        FROM @Table
        ) AS A
    CROSS APPLY String.nodes('/S') AS Split(a)

    RETURN
END

SELECT ROW_NUMBER()OVER(ORDER BY (SELECT 1))AS Seq,
    * FROM dbo.udf_Split('20030,20031,20033,20034,20065,20045,20044',',')

结果

Seq String
----------
1   20030
2   20031
3   20033
4   20034
5   20065
6   20045
7   20044