<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx</link><description>I have absolutely no idea why anyone wants to do this, but I keep answering the same question in forums: "How do I count the occurrences of a substring [note: usually comma] within a string?" In an effort to thwart carpal tunnel syndrome, I have created</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#1221</link><pubDate>Mon, 07 May 2007 04:59:56 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:1221</guid><dc:creator>beckham</dc:creator><description>&lt;p&gt;Good one..&lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#7342</link><pubDate>Tue, 17 Jun 2008 15:12:36 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:7342</guid><dc:creator>Jereme Guenther</dc:creator><description>&lt;p&gt;I am looking for this functionality, however I would prefer to do it without a Numbers table.&lt;/p&gt;
&lt;p&gt;My need is that of a search engine sproc. &amp;nbsp;I need to be able to weight the returned records and one of the items in the weighting system is to check how many times the search pattern occurs in the record.&lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#7873</link><pubDate>Wed, 16 Jul 2008 22:28:08 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:7873</guid><dc:creator>Pickles</dc:creator><description>&lt;p&gt;I need this too. I have a column that basically contains sql and I need to pull out field_ids from it, but each row has a different number of field_ids in the sql. Is there a different(better) solution?&lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#8003</link><pubDate>Wed, 23 Jul 2008 18:30:08 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:8003</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;I don't know why you would want to do it without the numbers table. &amp;nbsp;It makes the solution an order of magnitude faster.&lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#12088</link><pubDate>Mon, 23 Feb 2009 02:06:48 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:12088</guid><dc:creator>Craig Hathaway</dc:creator><description>&lt;p&gt;Hi Adam, I have tried this code but even the included examples do not work!&lt;/p&gt;
&lt;p&gt;SELECT dbo.GetSubstringCount('We like the autos, the autos that go boom.', 'auto', '')&lt;/p&gt;
&lt;p&gt;-- returns 0 &amp;nbsp; &amp;lt;-- should be 2&lt;/p&gt;
&lt;p&gt;SELECT dbo.GetSubstringCount('Autos are fun. &amp;nbsp;I like to drive my auto.', 'auto', '')&lt;/p&gt;
&lt;p&gt;-- returns 0 &amp;nbsp; &amp;lt;-- should be 2&lt;/p&gt;
&lt;p&gt;SELECT dbo.GetSubstringCount('Autos are fun. &amp;nbsp;I like to drive my auto.', 'auto', '%[a-z]%')&lt;/p&gt;
&lt;p&gt;-- returns 1 &amp;nbsp; &amp;lt;-- should be 1&lt;/p&gt;
&lt;p&gt;Is there a SQL setting / version dependency that O could be missing?&lt;/p&gt;
&lt;p&gt;Craig&lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#12108</link><pubDate>Mon, 23 Feb 2009 16:18:51 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:12108</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Hi Craig,&lt;/p&gt;
&lt;p&gt;Thanks for pointing this out. &amp;nbsp;It is indeed a version issue. &amp;nbsp;I wrote this back in the bad old days of SQL Server 2000, and apparently there was a change to the way PATINDEX works between 2000 and 2005. I just tested:&lt;/p&gt;
&lt;p&gt;SELECT PATINDEX('', ' ')&lt;/p&gt;
&lt;p&gt;SQL Server 2000 returns 0, whereas SQL Server 2005 returns 1. &amp;nbsp;This is breaking the third predicate in the WHERE clause within the function, which checks to see if the target string is prepended by anything that matches the input pattern. &lt;/p&gt;
&lt;p&gt;I'll have to think about how to fix this, but as a temporary workaround if you don't want to use exact-match mode, you could pass in some character that you know can't possibly exist in the target string:&lt;/p&gt;
&lt;p&gt;SELECT dbo.GetSubstringCount('We like the autos, the autos that go boom.', 'auto', CHAR(255))&lt;/p&gt;
&lt;p&gt;--Returns 2, even in SQL Server 2005 or 2008&lt;/p&gt;
&lt;p&gt;In the meantime, I'm wondering if this &amp;quot;new&amp;quot; behavior makes sense? &amp;nbsp;I'm not sure, but I'm leaning towards SQL Server 2000's answer. &amp;nbsp;An empty pattern shouldn't, in my opinion, match on anything at all...&lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#17296</link><pubDate>Tue, 06 Oct 2009 05:25:05 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17296</guid><dc:creator>Funmarkaz</dc:creator><description>&lt;p&gt;Great work dude!&lt;/p&gt;
&lt;p&gt;Can i use it on MyISAM?&lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#17321</link><pubDate>Tue, 06 Oct 2009 18:27:33 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17321</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Funmarkaz, yes, it should work with a bit of modification; MySQL's CREATE FUNCTION syntax isn't quite the same as SQL Server's.&lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#20328</link><pubDate>Tue, 29 Dec 2009 17:51:20 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:20328</guid><dc:creator>Mike Schafer</dc:creator><description>&lt;p&gt;I found this post because I was searching for a function for &amp;nbsp;a project where I need to find out how many delimiters exist in a string. &amp;nbsp;This implementation does not require a numbers table or anything other than the function itself. &amp;nbsp;There is no &amp;quot;additional bonus&amp;quot; feature in this version but it will count occurences without any additional db objects. Happy Querying!&lt;/p&gt;
&lt;p&gt;CREATE FUNCTION dbo.GetSubStringCount (	&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; @InputString NVARCHAR(4000), 	&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; @SearchString VARCHAR(255)&lt;/p&gt;
&lt;p&gt;)RETURNS INT WITH SCHEMABINDING AS &lt;/p&gt;
&lt;p&gt;BEGIN	&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; DECLARE @occurences AS BIGINT&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;,@position AS BIGINT&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @occurences = 0&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @position = 0&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; WHILE @position &amp;lt; LEN(@InputString)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; BEGIN&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; IF CHARINDEX(@SearchString, @InputString, @position) &amp;gt; 0&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BEGIN&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SET @occurences = @occurences + 1&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SET @position = CHARINDEX(@SearchString, @InputString, @position)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; END&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @position = @position+1&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; END&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; RETURN @occurences	&lt;/p&gt;
&lt;p&gt;END&lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#20379</link><pubDate>Wed, 30 Dec 2009 16:47:53 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:20379</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Mike,&lt;/p&gt;
&lt;p&gt;Thanks for sharing. A simpler and more efficient way to solve the problem (if you don't want the "bonus" feature) is to do:&lt;/p&gt;
&lt;p&gt;SELECT LEN(@InputString) - REPLACE(@InputString, @SearchString, '')&lt;/p&gt;
&lt;p&gt;I suspect that the numbers table will provide better performance than a WHILE loop, and both will be less efficient than the above solution, but I'll leave that testing as an exercise for anyone interested in taking this a bit further. &lt;/p&gt;&lt;p&gt;Even better would be to inline the Numbers table version (search my blog for my post on that topic), and a SQLCLR solution would probably be fastest of all. I would personally definitely keep the "bonus" around as it's been quite useful in a few projects I've worked on.&lt;br&gt;&lt;/p&gt;</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#24644</link><pubDate>Wed, 28 Apr 2010 20:57:05 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:24644</guid><dc:creator>Brian Lewis</dc:creator><description>&lt;p&gt;My reason for wanting this functionality: to count line breaks in sys.syscomments in order to measure how many lines of T-SQL there are in the project. The bonus feature will allow blank lines to be excluded from the count. &lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#24740</link><pubDate>Sat, 01 May 2010 15:49:25 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:24740</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Okay, the 2005/2008 bug is fixed.&lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#36174</link><pubDate>Fri, 10 Jun 2011 20:47:31 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:36174</guid><dc:creator>Jason </dc:creator><description>&lt;p&gt;Adam, thanks for posting this. &amp;nbsp; Very handy use of the numbers table. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I'm troubleshooting a system that is experiencing tempdb meta-data contention and trying to identify queries that are creating temp objects within ad-hoc sql statements. &amp;nbsp;I have traces of this activity and am using this to identify the 'worst offenders'. Initially I was just counting each query where TextData was like '%table%' until I found that some batches created 10-15 table variables, so I needed a way to count them.&lt;/p&gt;
&lt;p&gt;Works great. &amp;nbsp;thx.&lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#47806</link><pubDate>Tue, 19 Feb 2013 13:35:21 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47806</guid><dc:creator>Ron</dc:creator><description>&lt;p&gt;Adam,&lt;/p&gt;
&lt;p&gt;Is there a modification I can make to get:&lt;/p&gt;
&lt;p&gt;SELECT dbo.GetSubstringCount('ROUTINE MEDICAL EXAMINATION', 'NORMAL ROUTINE HISTORY AND PHYSICAL', '%[a-z]%')&lt;/p&gt;
&lt;p&gt;to return 1 (ROUTINE)&lt;/p&gt;
&lt;p&gt;and &lt;/p&gt;
&lt;p&gt;SELECT dbo.GetSubstringCount('REFLUX, ESOPHAGEAL', 'ESOPHAGEAL REFLUX', '%[a-z]%')&lt;/p&gt;
&lt;p&gt;to return 2 (ESOPHAGEAL &amp;amp; REFLUX)&lt;/p&gt;
</description></item><item><title>re: Counting occurrences of a substring within a string</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/counting-occurrences-of-a-substring-within-a-string.aspx#47807</link><pubDate>Tue, 19 Feb 2013 14:50:51 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47807</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Ron: Sure, it's doable, but it would be a completely different function. What you want to do is split both strings on any non-alpha character, then intersect the results. You can search my blog for a string splitter (the CLR version would probably be best), and then just use the INTERSECT operator to get your final answer.&lt;/p&gt;
&lt;p&gt;--Adam&lt;/p&gt;
</description></item></channel></rss>