We need a single select statement to get the count of particular character on a string.
For Eg.,
DECLARE @string varchar(50) = 'Microsoft SQL Server',
@SearchChar char(1) = 'r'
Its result should be '3'
SELECT LEN(@string) - LEN(REPLACE(@string,@SearchChar,''))
SELECT DATALENGTH(REPLACE (@string, @SearchChar, @SearchChar+@SearchChar)) - DATALENGTH (@string) As [Character Count]
No comments:
Post a Comment