-2

I have a list of concatenated values passed into a parameter in my stored procedure and I need to split these values.

I may have 30 values in my list , I have shown a few. I want to store them in temptable/table variable.

CREATE PROCEDURE SAMPLE (@LIST VARCHAR(MAX),@USERNUM BIGINT,@COUNTRYNO BIGINT)

EXEC SAMPLE
    'NAME:bankNO:branchNme:accountNbr:chequeddNbr:chequeddDte:payeeNme:branchCode'
    ,1,12001

Desired output:

sno  list            val1  val2  val3  val4  val5  val6 val7
1   1:2:3:4:5:6:7     1     2     3    4      5     6    7
Tanner
  • 22,205
  • 9
  • 65
  • 83
Ram
  • 47
  • 1
  • 5
  • 2
    Ok First of all remove the caps lock and retype the question with sample data and the expected output. – Mahesh Feb 20 '15 at 10:47
  • 1
    Please, don't use caps-lock when you write... Also, format code and sample data. – jarlh Feb 20 '15 at 10:47
  • 1
    Can you please modify the question in lower case :) – Kartic Feb 20 '15 at 10:48
  • I doubt I got the output format. Can you please show the exact output with respect to the input you are providing in SP. – Kartic Feb 20 '15 at 10:55
  • What do you exactly want? Show the sample data and expected output clearly.. – Saravana Kumar Feb 20 '15 at 10:56
  • sno list val1 val2 val3 val4 val5 val6 val7 1 1:2:3:4:5:6:7 1 2 3 4 5 6 7 – Ram Feb 20 '15 at 10:56
  • possible duplicate of [How to Split String by Character into Separate Columns in SQL Server](http://stackoverflow.com/questions/21991953/how-to-split-string-by-character-into-separate-columns-in-sql-server) – Tab Alleman Feb 20 '15 at 14:13

2 Answers2

0

You could use a standard string splitter, like DelimitedSplit8K by Jeff Moden: http://www.sqlservercentral.com/articles/Tally+Table/72993/

CREATE FUNCTION [dbo].[DelimitedSplit8K]
--===== 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
;

And use that for example with PIVOT if you need to get the data into separate columns:

SELECT 1 as sno, [1], [2], [3], [4], [5], [6], [7] 
FROM (
    SELECT 
        Item, 
        ItemNumber 
    from 
        dbo.DelimitedSplit8K ('A:B:C:1:2:3:4',':')
) AS S
PIVOT
(
    max(Item) FOR ItemNumber IN ([1], [2], [3], [4], [5], [6], [7])
) AS PivotTable;

Result:

sno 1   2   3   4   5   6   7
1   A   B   C   1   2   3   4
James Z
  • 12,209
  • 10
  • 24
  • 44
0

Use XML to split the list and PIVOT to convert the list to columns.

DECLARE @TempTable AS TABLE(Value VARCHAR(100), id int)
DECLARE @list AS VARCHAR(1000)

SET @list = 'NAME:bankNO:branchNme:accountNbr:chequeddNbr:chequeddDte:payeeNme:branchCode'

INSERT INTO @TempTable
 SELECT 
     Split.a.value('.', 'VARCHAR(100)') AS CVS , ROW_NUMBER() OVER(ORDER BY Split.a.value('.', 'VARCHAR(100)')) 
  FROM  
  (
    SELECT CAST ('<M>' + REPLACE(@list, ':', '</M><M>') + '</M>' AS XML) AS CVS 
  ) AS A CROSS APPLY CVS.nodes ('/M') AS Split(a)


  SELECT 1 as sno, @list AS List, * FROM @TempTable
  pivot(MAX(Value) FOR id in([1],[2],[3],[4],[5],[6],[7],[8])) AS Piv
Saravana Kumar
  • 3,669
  • 5
  • 15
  • 35