Solved

how to find substring between 3rd and 4th comma

Posted on 2010-11-10
3
1,005 Views
Last Modified: 2012-05-10
How to write a function which returns substring between commas.

example :
    @TempString = "25,134,315,124,248,15,"

select myfunction(@TempString, 3, ',')
should return : 315

select myfunction(@TempString, 4, ',')
should return : 124
0
Comment
Question by:Bharat Guru
3 Comments
 
LVL 9

Expert Comment

by:damerval
ID: 34104492
Hi,
This pseudocode should help you


function getStringAtCommaNumber (StringParm, commaPosition)
  Define Stringvar as string variable
  Define dynamic array of strings
  while (position of comma in string > 0)
     add (left part of string up to first comma position) to array
     Reassign whatever is right of the same position to stringvar
  end while
  if stringvar not empty then add stringvar to array
  return element of array at position commaposition - 1
end

The question is just 100 points after all :) I hope this helps. You will find all the reference information you need by looking up SQL server string functions (position, left, right, etc)

Thx

Philippe
0
 
LVL 13

Accepted Solution

by:
devlab2012 earned 100 total points
ID: 34104529
Here is the code for function that you need:


CREATE FUNCTION myfunction
(
@TempString varchar(50),
@position int,
@separator char(1)
) RETURNS varchar(50)
AS
BEGIN

declare @cp int
set @cp = 1
while (@cp < @position) 
begin
	set @TempString = substring(@TempString, charindex(@separator,@TempString) + 1,len(@TempString))
	set @cp = @cp + 1
end
set @TempString = substring(@TempString, 0, charindex(@separator, @TempString))
return @TempString
END

Open in new window

0
 

Author Closing Comment

by:Bharat Guru
ID: 34104939
I just updated following statement and it worked perfectly
set @cp = 0

Thannks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question