SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to make a split function for this parameter?


how to make a split function for this parameter?

Author
Message
Sivaganesh Tamilvendhan
Sivaganesh Tamilvendhan
Old Hand
Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)

Group: General Forum Members
Points: 320 Visits: 1399
here iam using a parameter

declare @Questions varchar(MAX)='1-2|32,42-41|44'

this should be splited in to three column



declare @Questions varchar(MAX)='1-32,42-41'
;WITH CTE1 AS(
SELECT id, LEFT(val, CHARINDEX('-', val)-1) AS QuestionId, SUBSTRING(val, CHARINDEX('-', val)+1, 100) AS [IndexNumber]
FROM [dbo].[FN_SplitData](@Questions,',')
)



select QuestionId,[IndexNumber] from CTE1


in which i receviwed output like this
QuestionId IndexNumber
1 32
42 41


using this split function

ALTER FUNCTION [dbo].[FN_SplitData](
@delimited NVARCHAR(MAX),
@delimiter NVARCHAR(100)
) RETURNS @t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))
AS
BEGIN
DECLARE @xml XML
SET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'

INSERT INTO @t(val)
SELECT r.value('.','varchar(MAX)') as item
FROM @xml.nodes('/t') as records(r)
RETURN
END


i just tried like this


declare @Questions varchar(MAX)='1-32|26,42-41|32'
;WITH CTE1 AS(
SELECT id, LEFT(val, CHARINDEX('-', val)-1) AS QuestionId, SUBSTRING(val, CHARINDEX('-', val)+1, 100) AS [IndexNumber]
, SUBSTRING(val, CHARINDEX('|', val)+1, 100) AS Numbers
FROM [dbo].[FN_SplitData](@Questions,',')
)

select QuestionId,[IndexNumber],Numbers from CTE1


which provieded output like this

QuestionId IndexNumber Numbers
1 32|26 26
42 41|32 32
but iam trying output like this

QuestionId IndexNumber Numbers
1 32 26
42 41 32




can any one plz try to help me to solve this....
subbu1
subbu1
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1049 Visits: 695
try below code....

declare @Questions varchar(MAX)='1-32|26,42-41|32'
--select * FROM [dbo].split(@Questions)

SELECT LEFT(val, CHARINDEX('-', val)-1) AS QuestionId
, substring(val,(CHARINDEX('-', val)+1),(CHARINDEX('|', substring(val,(CHARINDEX('-', val)+1),len(val)))-1)) AS [IndexNumber]
, SUBSTRING(val, CHARINDEX('|', val)+1, 100) AS Numbers
from FN_SplitData(@Questions,',')
Steven Willis
Steven Willis
SSC Eights!
SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)

Group: General Forum Members
Points: 855 Visits: 1721


DECLARE
@S VARCHAR(MAX)
,@Split1 CHAR(1)
,@Split2 CHAR(1)
,@Split3 CHAR(1)
,@X XML


/* User input */

SELECT
@S = '1-32|26,42-41|32'
,@Split1 = ','
,@Split2 = '-'
,@Split3 = '|'


/* Split the string and convert to XML */

SET @S = '<Question><QuestionID>'+REPLACE(@S,@Split1,'</Value></Question><Question><QuestionID>')
SET @S = REPLACE(@S,@Split2,'</QuestionID><IndexNum>')
SET @S = REPLACE(@S,@Split3,'</IndexNum><Value>')+'</Value></Question>'
SELECT @X = CONVERT(XML,'<root>' + @S + '</root>')


/* You could stop here and just display the data as XML */
SELECT @X


/* Display the results in a table */

DECLARE @XML_Temp TABLE (XML_Content XML)

INSERT INTO @XML_Temp VALUES (@X)
SELECT
Node.value('(QuestionID)[1]', 'varchar(50)') AS 'QuestionID'
,Node.value('(IndexNum)[1]', 'varchar(50)') AS 'IndexNum'
,Node.value('(Value)[1]', 'varchar(50)') AS 'Value'
FROM
@XML_Temp
CROSS APPLY
@X.nodes('root/Question/.') AS Content(Node)

/* Output:
QuestionId IndexNumber Value
1 32 26
42 41 32
*/



Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88584 Visits: 41130
subbareddy542 (10/18/2012)
try below code....

declare @Questions varchar(MAX)='1-32|26,42-41|32'
--select * FROM [dbo].split(@Questions)

SELECT LEFT(val, CHARINDEX('-', val)-1) AS QuestionId
, substring(val,(CHARINDEX('-', val)+1),(CHARINDEX('|', substring(val,(CHARINDEX('-', val)+1),len(val)))-1)) AS [IndexNumber]
, SUBSTRING(val, CHARINDEX('|', val)+1, 100) AS Numbers
from FN_SplitData(@Questions,',')


Not very useful unless one has a copy of "FN_SplitData". Please post it. Thanks.

Edit: Apologies and never mind... I see it further above in this thread.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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