In this blog post, we are going to see how can we easily find the number of occurrences of a Particular Character in the String Using T-SQL.
Finding Number of Occurrences of a Particular Character in the String
The Difference between the Length of String and Length of String without considering given particular character gives us the number of occurrences of a particular character in the string.
DECLARE @String VARCHAR(100), @Search_Char CHAR(1); SELECT @String = 'Arulmouzhi', @Search_Char='U'; SELECT CASE WHEN CHARINDEX(@Search_Char,@String)>0 THEN ( LEN(@String) - LEN(REPLACE(@String,@Search_Char,'')) ) ELSE 0 END AS Particular_Char_Count;
Thus we saw how to find the number of occurrences of a Particular Character in the String easily using T-SQL. The above works well in both Azure SQL db and On-Premises.