Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to find substring between 3rd and 4th comma

Posted on 2010-11-10
3
Medium Priority
?
1,032 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Expert Comment

by:Philippe 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 400 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

670 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