SQL Server find number of occurrences for a string

 Scenario:

List Employees with their the roles.
My User
Employee IDRoleID
11
22
31

Query:


DECLARE @searchString VARCHAR(1000)

SET @searchString = 'string' --Say 1

SELECT ( LEN(CAST([Data] AS VARCHAR(MAX))) - LEN(REPLACE(CAST([Data] AS VARCHAR(MAX)), @searchString, '')) ) / LEN(@searchString) [TotalCount],
       *
FROM   MyTable
ORDER  BY [TotalCount] DESC 

Output:


TotalCount
RoleID
21
12

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...