In this post, I am sharing one TSQL script to find count the number of occurrences of a string in SQL Server.
This is a very basic demonstration, but sometimes it helps a lot to do this kind basic analysis using TSQL.
Sometimes, we find that column like Reasons or Remarks has some duplicate words or strings. As we are using this data for our report’s purpose, so it needs to find the total number of occurrences for that string.
Based on this count, we can perform further operations like: update, delete of that duplicate words.
Below is a small demonstration of this:
First, Create a table with sample data:
CREATE TABLE tbl_FindStrings
INSERT INTO tbl_FindStrings
(1,'This is my first demo demo at dbrnd.com')
,(2,'My first demo demo at dbrnd.com demo page')
,(3,'This is my first demo')
,(4,'First demo at dbrnd.com demo page.')
,(5,'My demo page')
Script to find the occurrence of ‘demo’ word:
,COUNT = (LEN(DataString) - LEN(REPLACE(DataString, 'demo', '')))/LEN('demo')