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


Find the second comma


Find the second comma

Author
Message
shohelr2003
shohelr2003
Old Hand
Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)

Group: General Forum Members
Points: 339 Visits: 610
Dear,
I have a String='A,B,C'.

I want to find A, B, C individually using substing function. There may be 3 select statements. But I can't find the 2nd Comma(',') from that string.

Please help me.
vikasgoyal138
vikasgoyal138
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 6
You can use below script to split the string with comma

DECLARE @S varchar(max),
@Split char(1),
@X xml

SELECT @S = 'A,B,C',
@Split = ','

SELECT @X = CONVERT(xml,'<root><splittedvalues>' + REPLACE(@S,@Split,'</splittedvalues><splittedvalues>') + '</splittedvalues></root>')

SELECT Temp.splittedvalues.value('.','varchar(20)')
FROM @X.nodes('/root/splittedvalues') Temp(splittedvalues)
Steven Willis
Steven Willis
SSC Eights!
SSC Eights! (851 reputation)SSC Eights! (851 reputation)SSC Eights! (851 reputation)SSC Eights! (851 reputation)SSC Eights! (851 reputation)SSC Eights! (851 reputation)SSC Eights! (851 reputation)SSC Eights! (851 reputation)

Group: General Forum Members
Points: 851 Visits: 1721
shohelr2003 (5/12/2013)
Dear,
I have a String='A,B,C'.

I want to find A, B, C individually using substing function. There may be 3 select statements. But I can't find the 2nd Comma(',') from that string.

Please help me.


Try this funcion:



CREATE FUNCTION [dbo].[itvfFindPosTally]
(
@strInput VARCHAR(8000)
,@delimiter VARCHAR(5)
)
RETURNS TABLE WITH SCHEMABINDING
AS
RETURN
(
WITH E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
),
E2(N) AS (SELECT 1 FROM E1 a, E1 b
),
E4(N) AS (SELECT 1 FROM E2 a, E2 b
),
cteTally(N) AS (SELECT 0 UNION ALL
SELECT TOP (DATALENGTH(ISNULL(@strInput,1))) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
),
findchar (posnum,pos)
AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY t.N) AS posnum
,CHARINDEX(@delimiter,@strInput,t.N) AS pos
FROM
cteTally t
WHERE
(SUBSTRING(@strInput,t.N,1) = @delimiter)
)
SELECT
posnum
,pos
FROM
findchar
WHERE
pos > 0
)



sql crazy kid
sql crazy kid
SSC-Enthusiastic
SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)

Group: General Forum Members
Points: 124 Visits: 243
Hello dear,

please tryout this . I assume from your query string is not limited to 2 commas .

Declare @strsql varchar(50),@chrindex int
set @strsql = 'AA,BB,C,D'
select @strsql --print original val
select @chrindex=CHARINDEX(',',@strsql,1)
select SUBSTRING(@strsql,1,@chrindex-1)--output1

while @chrindex > 0
begin

set @strsql= SUBSTRING(@strsql,@chrindex+1,LEN(@strsql))

select @chrindex=CHARINDEX(',',@strsql,1)

if @chrindex >0 select SUBSTRING(@strsql,1,@chrindex-1) -- output2

end
select @strsql --output3
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40208 Visits: 38567
atheeth (5/12/2013)
Hello dear,

please tryout this . I assume from your query string is not limited to 2 commas .

Declare @strsql varchar(50),@chrindex int
set @strsql = 'AA,BB,C,D'
select @strsql --print original val
select @chrindex=CHARINDEX(',',@strsql,1)
select SUBSTRING(@strsql,1,@chrindex-1)--output1

while @chrindex > 0
begin

set @strsql= SUBSTRING(@strsql,@chrindex+1,LEN(@strsql))

select @chrindex=CHARINDEX(',',@strsql,1)

if @chrindex >0 select SUBSTRING(@strsql,1,@chrindex-1) -- output2

end
select @strsql --output3



I think you will find the function posted above by Steven Willis to be faster and more scaleable than what you have posted. Try them both out against a million row table.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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