Gets the number of occurrences of a string in a long string in SQL
Let’s say I have a string:
X-BGS-2010-09-15-001
I want to know how many times the ’-’ occurs, which can be done in the following way, without the need for complex 1-by-1 character analysis.
declare @a varchar(100) set @a=‘X-BGS-2010-09-15-001’ select len(replace(@a,’-’,’—’))-len(@a)
If you want to determine how many letters c are in the b field in a, you can write it like this
select len(replace(b,‘c’,’—’)) -len (b) from a, of course, the function len may vary from database to database