Results 1 to 3 of 3
  1. #1
    Join Date
    May 2004
    Posts
    144

    Unanswered: count number of character occurrrnce i a string

    hi
    how can i select number of a character occurrrnce in a string?
    for example in "bono", number of "o" is 2.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    use the REPLACE function to remove the character from the column value (by replacing it with an empty string)

    compare the length of the replaced string with the length of the original column value

    the difference is the number of characters that were removed, i.e. the number of characters in the original
    Code:
    select length('bono') - length(replace('bono','o','')) as number_of_characters
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    That's nice rudy!

Posting Permissions

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