Skip to content
Advertisement

How can you find the number of occurrences of a particular character in a string using sql?

How can you find the number of occurrences of a particular character in a string using sql?

Example: I want to find the number of times the letter ā€˜dā€™ appears in this string.

declare @string varchar(100)
select @string = 'sfdasadhfasjfdlsajflsadsadsdadsa'

Advertisement

Answer

Here you go:

declare @string varchar(100)
select @string = 'sfdasadhfasjfdlsajflsadsadsdadsa'
SELECT LEN(@string) - LEN(REPLACE(@string, 'd', '')) AS D_Count
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement