Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 February SQL Server: TSQL Script to find Count the number of Occurrences of a String

SQL Server: TSQL Script to find Count the number of Occurrences of a String

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a TSQL script to 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 of the duplicate string.

Further, we can perform operations on this like update, delete of that duplicate words.

Below is a small demonstration of this:

Create a table with sample data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CREATE TABLE tbl_FindStrings
(
ID INTEGER
,DataString VARCHAR(255)
)
GO
 
INSERT INTO tbl_FindStrings
VALUES
(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')
GO

Script to find the occurrence of ‘demo’ word:

1
2
3
4
5
6
SELECT
ID
,DataString
,COUNT = (LEN(DataString) - LEN(REPLACE(DataString, 'demo', '')))/LEN('demo')
FROM tbl_FindStrings
GO

The Result:

SQL Server String Occurrence

Feb 23, 2016Anvesh Patel
MySQL: Script to identify the locks and blocking transactionsMySQL: Error 1153 - Got a packet bigger than 'max_allowed_packet' bytes
Comments: 5
  1. bayu
    January 10, 2017 at 4:27 am

    Thank You Bro

  2. James Smith
    April 18, 2018 at 12:08 pm

    Very clever and a great little tools. THANKS and CHEERS

  3. John Doe
    August 3, 2018 at 9:35 pm

    brilliantly clever!

  4. Michael
    August 10, 2018 at 8:20 pm

    This is exactly what I needed. I was building toward this idea, but lacked the time to finish the puzzle. Well done and thank you.

  5. Michael
    September 10, 2018 at 11:25 am

    what if i need to find only two occurrence of ‘demo’ word

Anvesh Patel
Anvesh Patel

Database Engineer

February 23, 2016 SQL ServerAnvesh Patel, database, database research and development, dbrnd, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, String occurrence, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....