Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

tighter approach to counting strings in strings

By John,

Saw Mike's routine for counting occurences of a string in a string and I thought I'd offer a simpler solution.

Simply replace the occurence with null and use length differences as shown.

NOTE: len may ignore trailing spaces, so this can be made more solid by adding some non printable char (less likely to interfere with operation) to the end of the string being searched. (e.g. 'seach string here     ' ->
'seach string here     $' )

This can be wrapped in a function for utility.

Total article views: 61 | Views in the last 30 days: 1
Related Articles

string Comparison.

'string' = 'string___' ???


Manipulate String

String Output


csv string

csv string


string formatting

String Formatting


parse/split string

string manipulation