| |
|
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.
|
 |

01-03-10, 14:10
|
|
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
|
|

01-03-10, 14:14
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
hint: use INSTR inside REVERSE
|
|

01-03-10, 14:53
|
|
Registered User
|
|
Join Date: Mar 2007
Posts: 194
|
|
|
|
Quote:
Originally Posted by r937
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;
|
|

01-03-10, 14:58
|
|
Registered User
|
|
Join Date: Mar 2007
Posts: 194
|
|
Quote:
Originally Posted by ozzii
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]
|
|

01-03-10, 17:18
|
|
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.
|
|

01-03-10, 17:40
|
|
Registered User
|
|
Join Date: Mar 2007
Posts: 194
|
|
Quote:
Originally Posted by mike_bike_kite
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`
|
|

01-03-10, 19:42
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
Quote:
Originally Posted by ozzii
Actually what I wanted was to count the occurrances of a given character. I should have explained it better.
|
you can say that again!!

|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|