Click here to monitor SSC
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
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
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
SSC Eights!
SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)

Group: General Forum Members
Points: 947 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-Addicted
SSC-Addicted (479 reputation)SSC-Addicted (479 reputation)SSC-Addicted (479 reputation)SSC-Addicted (479 reputation)SSC-Addicted (479 reputation)SSC-Addicted (479 reputation)SSC-Addicted (479 reputation)SSC-Addicted (479 reputation)

Group: General Forum Members
Points: 479 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-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45127 Visits: 39923
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

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