Click here to Skip to main content
16,004,587 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Is there a function that counts how many times a sub-string appears within the larger string.

Example: "Recover your contacts on your phone system"

The function would need to tell me how many times "your" appears in the string; which would be 2.
Posted
Comments
Sergey Alexandrovich Kryukov 10-May-13 16:58pm    
How can this be related to database? You could better do it in your application's language.
—SA

(Length('Find your own way in your own world') - Length(Replace('Find your own way in your own world','your',''))) / Length('your')

...which correctly returned 2.


This seemed to work better than the one suggested on the PostgreSQL web site...


Length('Find your own way in your own world') - Length(regexp_replace('Find your own way in your own world','your','','g')) / Length('Find your own way in your own world')

...which returns 35 (...this may have something to do with the 'g' in the expression but it's not explained do I just left it.)
 
Share this answer
 
Comments
RaisKazi 10-May-13 21:20pm    
Glad to know you found solution. I haven't got chance to test code from that link since I do not have PostgreSQL. But I think it provided you direction.
Have a look at below simmilar Question-Answer thread.

http://www.postgresql.org/message-id/20091020172452.GA10593@tux

Hope it helps.
 
Share this answer
 
Comments
Maciej Los 10-May-13 17:44pm    
+5
RaisKazi 10-May-13 21:21pm    
Thank you Maciej, :).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900