count number occurrences of char value

Script to count 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

Comments

There were no comments found for this weblog.

Du mußt Dich einloggen um diesen Weblog kommentieren zu können!