Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

can any one tell me how to split the given @parameter data into three column of table? Expand / Collapse
Author
Message
Posted Sunday, August 26, 2012 11:45 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 13, 2014 1:11 AM
Points: 212, Visits: 1,385
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
ss	col	col1
1 2_3 2_3
4 5_6 5_6

plz tell me how to get output like this
ss	col	col1
1 2 3
4 5 6

Post #1350204
Posted Monday, August 27, 2012 4:06 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1350297
Posted Monday, August 27, 2012 12:22 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 13, 2014 1:11 AM
Points: 212, Visits: 1,385
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
ss	col	col1
1 2 3
4 5 NULL


and trying output like this
ss	col	col1
1 2 3
4 5 0

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

Post #1350520
Posted Monday, August 27, 2012 1:43 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:56 PM
Points: 13,078, Visits: 12,529
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
ss	col	col1
1 2 3
4 5 NULL


and trying output like this
ss	col	col1
1 2 3
4 5 0

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1350556
Posted Monday, August 27, 2012 1:45 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:56 PM
Points: 13,078, Visits: 12,529
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1350558
Posted Tuesday, August 28, 2012 11:04 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 13, 2014 1:11 AM
Points: 212, Visits: 1,385
thanks
Sean Lange
Post #1351385
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse