Results 1 to 5 of 5

Thread: substring count

  1. #1
    Join Date
    Feb 2004
    Location
    Bucharest
    Posts
    37

    Post Unanswered: 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

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    you think it is slow? it isn't
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

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

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    what's inelegant about it? what would a more elegant function look like?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •