SQL Server: Count Number of Occurrences of a Character or Word in a String

CODE

CODE

DECLARE @string VARCHAR(MAX)='Noida, short for the New Okhla Industrial Development Authority, is a planned city in India under the management of the New Okhla Industrial Development Authority.It is part of National Capital Region of India.

  1. DECLARE @tosearch VARCHAR(MAX)='In'  
  2.   
  3. SELECT (DATALENGTH(@string)-DATALENGTH(REPLACE(@string,@tosearch,'')))/DATALENGTH(@tosearch)  
  4. AS OccurrenceCount  

 

Ebook Download
View all
Learn
View all