Solved

how to find substring between 3rd and 4th comma

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

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

735 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