Fetch the number of occurrences of ‘s’ in each employee name

Posted by Bandi under Sql Server category on | Points: 40 | Views : 155
If you want to find out 'How many number of times a character/word/special symbol repeated in a string', we can use below query,

SELECT (LEN(FirstName)- LEN(REPLACE(FirstName,'s','')) FROM EmployeeTable 

LEN(FirstName) -- gives the length/number of characters of a string/Column ( i.e. FirstName)

LEN(REPLACE(FirstName,'s','')) -- firstly it replaces the character 'S' with empty string and then gives the length of string without character 'S'

(Total Length) - (String Length with out 'S') gives the number of occurances of a character in s string

Comments or Responses

Login to post response