If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > How can i find last occurence of a substring in string?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-03-10, 14:10
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
How can i find last occurence of a substring in string?

Hi

I can use MySQL INSTR(str,substr) function to return the position of the first occurrence of substring substr in string str. Does any one know if there is a function or a way to return the the position of the last occurrence of substring substr in string str?

Regards
Reply With Quote
  #2 (permalink)  
Old 01-03-10, 14:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
hint: use INSTR inside REVERSE
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-03-10, 14:53
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Quote:
Originally Posted by r937 View Post
hint: use INSTR inside REVERSE
not sure how you can do a work around using REVERSE but i've managed the follwoing workaround.

Code:
select instr('1,2,3,4,5', substring_index('1,2,3,4,5', ',', -1)) - 1 col;
Reply With Quote
  #4 (permalink)  
Old 01-03-10, 14:58
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Quote:
Originally Posted by ozzii View Post
not sure how you can do a work around using REVERSE but i've managed the follwoing workaround.

Code:
select instr('1,2,3,4,5', substring_index('1,2,3,4,5', ',', -1)) - 1 col;
Actually the above wont work if I have the following - its counting each character:

Code:
select instr('10,2,3,4,5', substring_index('1,2,3,4,5', ',', -1)) - 1 col;
[/QUOTE]
Reply With Quote
  #5 (permalink)  
Old 01-03-10, 17:18
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Can you explain why you're trying to do this and provide a little example data. I'm guessing that the db design is a bit suspect.
Reply With Quote
  #6 (permalink)  
Old 01-03-10, 17:40
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Quote:
Originally Posted by mike_bike_kite View Post
Can you explain why you're trying to do this and provide a little example data. I'm guessing that the db design is a bit suspect.
Actually what I wanted was to count the occurrances of a given character. I should have explained it better. Any way I've found the following which works a treat:

Code:
SELECT LENGTH('10,2,3,4,5') - LENGTH(REPLACE('10,2,3,4,5', ',', '')) AS `occurrences`
Reply With Quote
  #7 (permalink)  
Old 01-03-10, 19:42
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by ozzii View Post
Actually what I wanted was to count the occurrances of a given character. I should have explained it better.
you can say that again!!

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On