SQL查询从字符串中获取多个“,”位置

时间:2016-11-24 11:58:28

标签: sql sql-server tsql charindex patindex

我有5行数据,如下所示

Image 1

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

我的输出应该是这样的:

Image 2

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 

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