x
login about faq Site discussion (meta-askssc)

I need to get the sub string in between the nth & mth occurrence of a character in a single select statement

Hi,
I need to get the sub string in between the nth & mth occurrence of a character.
Example,from the sting '1:12.85:3:4' I need the sub string in between the first & second occurrence of the character ':' in a single select statement.

Thanks in advance..
Regards,
Nidheesh

more ▼

asked Jul 19 '11 at 08:33 PM in Default

nidheesh gravatar image

nidheesh
90 6 10 12

(comments are locked)
10|1200 characters needed characters left

3 answers: sort voted first

Your question is the same as this one so the answers will apply to your situation http://ask.sqlservercentral.com/questions/73911/query-to-select-a-string-between-two-known-strings

more ▼

answered Jul 20 '11 at 02:13 PM

Fatherjack gravatar image

Fatherjack ♦♦
38.8k 56 73 104

(comments are locked)
10|1200 characters needed characters left

As I posted several times on this site, the easiest way to solve such scenarios is to use the CLR RegEx implementation.

You can take a look on the sample implementation of CLRRegEx on my site: SQL Server 2005 and SQL Server 2008 Regular Expressions and pattern matching

Using the sample implementation you can receive result by simple query using a simple regular expression: :(.+?):

SELECT [dbo].[fn_RegExMatch]('1:12.85:3:4', ':(.+?):', 1, 1)
more ▼

answered Jul 21 '11 at 12:13 AM

Pavel Pawlowski gravatar image

Pavel Pawlowski
20.3k 5 10 20

(comments are locked)
10|1200 characters needed characters left

IN THE REQUIREMENT YOU MENTIONED THE CHAREACTER 1 IS AT TWO PLACES, IF THE CHARACTER 1 IS PRESENT AT THREE PLACES MEANS THEN HOW DO YOU WANT YOUR SUBSTRING, 1 -- SUBSTRING BETWEEN 1ST 1 AND 2ND 1 2 -- SUBSTRING BETWEEN 1ST 1 AND 3RD 1

LIKE ''1:12.81:3:4' THEN HOW DO YOU CONSIDER

more ▼

answered Jul 19 '11 at 09:14 PM

Naveen Kumar gravatar image

Naveen Kumar
134 8 9 13

I think that's not the issue with this requirement.Is there any logic to get the mth & nth occurence of a character in a string by using a single select statement with out using other usd fun?.

Jul 19 '11 at 09:21 PM nidheesh

check it out this one, select SUBSTRING('1:12.85:3:4',len('1:12.85:3:4')-charindex('1',reverse('1:12.85:3:4')),1)

Jul 20 '11 at 12:50 AM Naveen Kumar

select SUBSTRING('1:12.85:3:4',len('1:12.85:3:4')-charindex('1',reverse('1:12.85:3:4')),1)

in the select if you want the occurances between ":" charachter just replace 1 with ":" in charindex function...

hope this helps you...

Jul 20 '11 at 12:17 PM Naveen Kumar
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x916
x10

asked: Jul 19 '11 at 08:33 PM

Seen: 1693 times

Last Updated: Jul 21 '11 at 02:19 PM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.