-5

I have 5 rows of data like as below

Image 1

Now I need to find the position of every ',' from my input string.

My output should be like this:

Image 2

halfer
  • 19,824
  • 17
  • 99
  • 186
Akhil
  • 41
  • 3
  • 10
  • 1
    What's the limit of how many commas you'll have? – Rich Benner Nov 24 '16 at 11:59
  • Hi @RichBenner - Thanks for replying. Currently I have only 10 commas. But May be there is a possibility of increasing the commas. In that case also the code should work accordingly – Akhil Nov 24 '16 at 12:06
  • 1
    Why do you need to find the position of the commas? What are you doing with this information once you have found it? – iamdave Nov 24 '16 at 12:07
  • Normalise the data before you feed it into SQL Server. But, failing that: http://stackoverflow.com/a/3735188/242520 – ta.speot.is Nov 24 '16 at 12:09
  • @ta.speot.is - Thanks for your reply. I'm not dumping the data to SQL using ExCEL. The data is loading through Salesforce. – Akhil Nov 24 '16 at 12:11
  • @iamdave - Thanks for your reply. This is a part of my requirement. Just for a example I mentioned as comma. – Akhil Nov 24 '16 at 12:13
  • 1
    @AkhilDuvvuru I understand that this is a part of your requirement, I am asking *why* is it a part of your requirement? It looks like you are trying to split out the ID values? If this is the case, this can be done in a much better way than what you are trying to do here. – iamdave Nov 24 '16 at 12:15
  • @iamdave - I'm not trying to split out the ID values. But I'm trying find the position or the delimiter for tracking report. – Akhil Nov 24 '16 at 12:19
  • @iamdave But not exactly like this. I need to find all the commas in a single string as I posted in the Output screenshot. – Akhil Nov 24 '16 at 12:24
  • Note that [ASAP begging is discouraged](http://meta.stackoverflow.com/q/326569) here, please do not add it or start edit wars to insist upon it. – halfer Nov 24 '16 at 18:19

2 Answers2

0

It looks like you are trying to split your ID values out of comma separated lists. If this is the case you would be better served creating a table-valued function that splits your comma separated list into rows.

You can use Jeff Moden's function below to achieve this using the following:

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

Which will return the following:

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's String Split function:

/****** 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
iamdave
  • 12,023
  • 3
  • 24
  • 53
  • But not exactly like this. I need to find all the commas in a single string as I posted in the example screenshot – Akhil Nov 24 '16 at 12:22
  • @AkhilDuvvuru What is the purpose of the tracking report? – iamdave Nov 24 '16 at 12:28
  • @John Cappelletti - Sorry- I just noticed that I have following characters in the the data () & / : # + – Akhil Nov 25 '16 at 13:54
0

Please try this one it will give output as yours.

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 

This will show proper output just put it in temp table and display it.

Husen
  • 1,541
  • 10
  • 14
  • The input data is not only in the form of INTEGERS but also in the form of Varchar. (For Ex TOLL2163NSW,TOLL4115QLD,TOLL3025VIC,TOLL6105WA) – Akhil Nov 24 '16 at 13:11
  • I'm getting the data from system_client_id column which is in contracts_lu table. The input can be in Varchar as I stated the above example. And I'm pushing that data into a temp table as #Tracking. From that I have to find the position of the comma. – Akhil Nov 24 '16 at 13:15
  • Your solution is almost close. But can you please help you with the solution with my req. – Akhil Nov 24 '16 at 13:16
  • INTEGERS of Varchar don't matter, it will work for both.You have to just insert in temp table and return it. – Husen Nov 25 '16 at 09:15
  • Sure. Is it possible to split the IDs based on comma without locating the position like I have a ID's from Input and I have to split it to different columns as below screenshot. [Something like this][1] [1]: i.stack.imgur.com/HQzPc.jpg – Akhil Nov 25 '16 at 09:18
  • I want my output should be like this. Is it possible? And It should be Dynamic as pictured in the below [screenshot][1]. P.S: ID's are comming from a table [1]: https://i.stack.imgur.com/w5OLQ.jpg – Akhil Nov 25 '16 at 10:39