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 > substring count

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-13-06, 10:49
Zamolxe Zamolxe is offline
Registered User
 
Join Date: Feb 2004
Location: Bucharest
Posts: 37
Post substring count

hello guys

i am trying to count the number of a substring occurences in a string. much like subtr_count

i found the following method, but i don't like it, and i think it is slow


Code:
SELECT ( LENGTH('i use mysql since 2003 and i like the sql syntax. sql means structured query language') - LENGTH(REPLACE('i use mysql since 2003 and i like the sql syntax. sql means structured query language','sql', '')) ) / LENGTH('sql') AS `len`
please someone point me to a function or another combination of functions, that will work more efficient and faster

thanks
__________________
My Blog
Reply With Quote
  #2 (permalink)  
Old 06-13-06, 10:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
you think it is slow? it isn't
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-13-06, 10:58
Zamolxe Zamolxe is offline
Registered User
 
Join Date: Feb 2004
Location: Bucharest
Posts: 37
re

if you say so i believe you

i thought there is an elegant solution to this in mysql, it seems that it doesn't have a predefined function yet
__________________
My Blog
Reply With Quote
  #4 (permalink)  
Old 06-13-06, 11:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
what's inelegant about it? what would a more elegant function look like?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 06-13-06, 12:01
Zamolxe Zamolxe is offline
Registered User
 
Join Date: Feb 2004
Location: Bucharest
Posts: 37
Post re

i ment that you are doing to many operations:
- counting length of the main string
- replacing the substring with nothing in the main string
- counting length of the resulted main string
- the difference of the 2 main strings per length of the substring => nr of occurences

looks complicated to me

if this is the only option available then i'll use this

i hope mysql will pop a new substr_count function in the coming versions
__________________
My Blog
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