Count the number of occurrence of a character in a given string – SQL

Count the number of occurrence of a 'o' in the given string: Ram is a good boy.

DECLARE @vc_string varchar(40)
SET @vc_string = 'Ram is a good boy.'

SELECT LEN(@vc_string) - LEN(REPLACE(@vc_string, 'o', '')) AS in_count

Output: 3

No comments:

Post a Comment