In this blog post, we will see how can we easily find the number of occurrences of a Particular Word inside the String Using T-SQL.
Check our previous post for Finding Number of Occurrences of a Particular Character in the String.
Finding Number of Occurrences of a Particular Word inside the String
The Difference between the Length of String and Length of String without considering given particular word divided by the Length of Search String gives us the number of occurrences of a particular word inside the string.
DECLARE @String VARCHAR(100), @Search_String VARCHAR(100); SELECT @String = 'How are you? We are fine.', @Search_String='are'; SELECT CASE WHEN CHARINDEX(@Search_String,@String)>0 THEN ( LEN(@String) - LEN(REPLACE(@String,@Search_String,'')) ) / LEN(@Search_String) ELSE 0 END AS Particular_Word_Count;
Thus we saw how to find the number of occurrences of a Particular Word inside the String easily using T-SQL. The above works well in both Azure SQL db and On-Premises.