can any one tell me how to split the given @parameter data into three column of table?

  • this is way i tried to split data in parameter

    declare @parameter varchar (200)

    set @parameter ='1_2_3|4_5_6'

    ;WITH CTE1 AS(

    SELECT LEFT(items, CHARINDEX('_', items)-1) AS ss, SUBSTRING(items, CHARINDEX('_', items)+1 ,100) as col,SUBSTRING(items, CHARINDEX('_', items)+1 ,100) as col1

    FROM dbo.characterSplit(@parameter,'|')

    )

    --INSERT INTO tblAdBlock(AdSpaceId ,Row ,[Column])

    SELECT *

    FROM CTE1 ;

    this is function which i used to split the '|'

    ALTER FUNCTION [dbo].[characterSplit](@String varchar(8000), @Delimiter char(1))

    returns @temptable TABLE (items varchar(8000))

    as

    begin

    declare @idx int

    declare @slice varchar(8000)

    select @idx = 1

    if len(@String)<1 or @String is null return

    while @idx!= 0

    begin

    set @idx = charindex(@Delimiter,@String)

    if @idx!=0

    set @slice = left(@String,@idx - 1)

    else

    set @slice = @String

    if(len(@slice)>0)

    insert into @temptable(Items) values(@slice)

    set @String = right(@String,len(@String) - @idx)

    if len(@String) = 0 break

    end

    iam getting output like this

    sscolcol1

    12_32_3

    45_65_6

    plz tell me how to get output like this

    sscolcol1

    123

    456

  • First, start with a better string splitter, like the one from Jeff Moden:

    http://www.sqlservercentral.com/articles/Tally+Table/72993/

    Then use cascading CROSS APPLYs, thusly:

    declare @parameter varchar (200)

    set @parameter ='1_2_3|4_5_6'

    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


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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

  • 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/

  • More importantly than getting the desired output....do you understand what that function does? Can you explain it somebody else? Keep in mind that YOU are the person who has to support this code at 3am when the phone rings.

    _______________________________________________________________

    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/

  • thanks

    Sean Lange

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply