Home Forums SQL Server 2008 SQL Server 2008 - General can any one tell me how to split the given @parameter data into three column of table? RE: can any one tell me how to split the given @parameter data into three column of table?

  • sivajii (8/27/2012)


    thanks

    dwain.c

    it was working fine

    can u plz give one idea is there any chance to avoid null value instead of that replace 0 there in column in this proc itself

    declare @parameter varchar (200)

    set @parameter ='1_2_3|4_5'

    SELECT ss=MAX(CASE c.itemnumber WHEN 1 THEN c.item END)

    ,col=MAX(CASE c.itemnumber WHEN 2 THEN c.item END)

    ,col1=MAX(CASE c.itemnumber WHEN 3 THEN c.item END)

    FROM (SELECT @parameter) a(parameter)

    CROSS APPLY dbo.DelimitedSplit8k(parameter, '|') b

    CROSS APPLY dbo.DelimitedSplit8k(item, '_') c

    GROUP BY b.ItemNumber

    i am getting output like this

    sscolcol1

    123

    45NULL

    and trying output like this

    sscolcol1

    123

    450

    i tried like this

    declare @parameter varchar (200)

    set @parameter ='1_2_3|4_5'

    SELECT ss=MAX(CASE c.itemnumber WHEN 1 THEN c.item END)

    ,col=MAX(CASE c.itemnumber WHEN 2 THEN c.item END)

    ,col1=MAX(CASE c.itemnumber WHEN 3 THEN ISNULL(c.item,0) END)

    FROM (SELECT @parameter) a(parameter)

    CROSS APPLY dbo.DelimitedSplit8k(parameter, '|') b

    CROSS APPLY dbo.DelimitedSplit8k(item, '_') c

    GROUP BY b.ItemNumber

    You have your isnull check in the wrong spot. You have it inside your case which doesn't do what you want because there is no value in the second row where ItemNumber = 3.

    declare @parameter varchar (200)

    set @parameter ='1_2_3|4_5'

    SELECT ss=isnull(MAX(CASE c.itemnumber WHEN 1 THEN c.item END), 0)

    ,col=isnull(MAX(CASE c.itemnumber WHEN 2 THEN c.item END), 0)

    ,col1=isnull(MAX(CASE c.itemnumber WHEN 3 THEN c.item END), 0)

    FROM (SELECT @parameter) a(parameter)

    CROSS APPLY dbo.DelimitedSplit8k(parameter, '|') b

    CROSS APPLY dbo.DelimitedSplit8k(item, '_') c

    GROUP BY b.ItemNumber

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/