-5

我有 5 行数据,如下所示

图 1

现在我需要从我的输入字符串中找到每个“,”的位置。

我的输出应该是这样的:

图 2

4

2 回答 2

0

看起来您正试图将您的ID值从逗号分隔的列表中拆分出来。如果是这种情况,您最好创建一个表值函数,将逗号分隔的列表拆分为行。

您可以使用以下 Jeff Moden 的函数来实现此目的:

select i.ID
        ,ItemNumber
        ,Item as IDSplit
from Input i
    cross apply dbo.DelimitedSplit8K(i.ID,',') s;

这将返回以下内容:

ID                                          | ItemNumber | IDSplit
````````````````````````````````````````````|````````````|`````````
21321,32154,84655,65465,65476,89799,65464   | 1          | 21321
21321,32154,84655,65465,65476,89799,65464   | 2          | 32154
21321,32154,84655,65465,65476,89799,65464   | 3          | 84655
21321,32154,84655,65465,65476,89799,65464   | 4          | 65465
21321,32154,84655,65465,65476,89799,65464   | 5          | 65476
21321,32154,84655,65465,65476,89799,65464   | 6          | 89799
21321,32154,84655,65465,65476,89799,65464   | 7          | 65464
21313,32156,31656,32132                     | 1          | 21313
21313,32156,31656,32132                     | 2          | 32156
21313,32156,31656,32132                     | 3          | 31656
21313,32156,31656,32132                     | 4          | 32132

Jeff Moden 的字符串拆分函数:

/****** Object:  UserDefinedFunction [dbo].[DelimitedSplit8K]    Script Date: 24/11/2016 12:08:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[DelimitedSplit8K]
--===== String Split function by Jeff Moden: http://www.sqlservercentral.com/articles/Tally+Table/72993/
--===== Define I/O parameters
        (@pString VARCHAR(8000), @pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE!  IT WILL KILL PERFORMANCE!
RETURNS TABLE WITH SCHEMABINDING AS
 RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
     -- enough to cover VARCHAR(8000)
  WITH E1(N) AS (
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ),                          --10E+1 or 10 rows
       E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
       E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
 cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
                     -- for both a performance gain and prevention of accidental "overruns"
                 SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
                 SELECT 1 UNION ALL
                 SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                ),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
                 SELECT s.N1,
                        ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                   FROM cteStart s
                )
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
 SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
        Item       = SUBSTRING(@pString, l.N1, l.L1)
   FROM cteLen l
于 2016-11-24T12:19:20.577 回答
0

请试试这个,它会给你的输出。

Create table #Table (rowNo int identity(1,1), ID varchar(100))
insert into #Table values('32132')
insert into #Table values('32132,32132')
insert into #Table values('32132,32132,6456,654,645')

declare @TableRow int = (select count(*) from #Table),@Tableloop int = 1
while(@Tableloop <= @TableRow)
begin
    Declare @var varchar(100) ;
    SET @var = (select ID from #Table where rowNo=@Tableloop)
    declare @count int = (select len(@var) - len(replace(@var, ',', '')))
    declare @loop int = 1;
    declare @location int = 0;
    print 'Row' + cast(@Tableloop as varchar(5))
    while (@loop <= @count)
    begin
        SET @location = (select charindex(',',@var,@location))
        print cast(@loop as varchar(5)) + ' Comma at ' + cast(@location as varchar(5))
        SET @location = @location +1
        SET @loop = @loop + 1;
    end

    SET @Tableloop = @Tableloop + 1;
END
drop table #Table 

这将显示正确的输出,只需将其放入临时表并显示即可。

于 2016-11-24T12:24:22.360 回答