Solved

how to find substring between 3rd and 4th comma

Posted on 2010-11-10
3
996 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
Comment Utility
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
Comment Utility
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
Comment Utility
I just updated following statement and it worked perfectly
set @cp = 0

Thannks
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now