Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
Sivaganesh Tamilvendhan
Sivaganesh Tamilvendhan
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 1399
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


dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4255 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Sivaganesh Tamilvendhan
Sivaganesh Tamilvendhan
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 1399
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


Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16586 Visits: 17024
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)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16586 Visits: 17024
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)
Sivaganesh Tamilvendhan
Sivaganesh Tamilvendhan
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 1399
thanks
Sean Lange
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search