Wednesday, November 21, 2007

[mssql] Count Number of Certain Character in A String

Some times we need to know number of certain character in a field, for example to know:
  • How deep the directory path by calculating the number of backslash in a path.
  • How many item in a list of string by calculating number of comma in the string.
Following the statements for that purpose:
DECLARE @cPath varchar(100)

SET @cPath = 'D:\TEMP\test.txt'
SELECT len(@cPath) - len(replace(@cPath, '\', ''))

No comments:

Post a Comment