Solved

how to find substring between 3rd and 4th comma

Posted on 2010-11-10
3
1,022 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 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

631 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