Skip to main content

Count the number of characters in a given string in Transact SQL.

--
-- Count Character Occurrences
--
-- To count the number of characters in a given string.
--

DECLARE @count int
DECLARE @strValue varchar(100)
DECLARE @searchCharacter varchar(1)

SET @strValue = 'We are looking for an "e"!'
SET @searchCharacter = 'e'

SELECT @count = LEN(@strValue) - LEN(REPLACE(@strValue, @searchCharacter, ''))
PRINT @count -- @count = 3