How to count the occurrence of specific word inside a string in sql server 2008?


Hi. Suppose, I have for example this string field in my sql database  "never say never"  and I want to count how many times "never" appear in the string in sql server 2008, so how I can check that.

Thanks.


Asked by:- LuneAgile
1
: 3132 At:- 8/7/2018 2:30:01 PM
SQL count the occurrence of specific word







2 Answers
profileImage Answered by:- vikas_jk

Here is the working example

    Declare @string VARCHAR(MAX) ='Never say Never'
	DECLARE @tosearch VARCHAR(MAX)='Never'  
      
    SELECT (DATALENGTH(@string)-DATALENGTH(REPLACE(@string,@tosearch,'')))/DATALENGTH(@tosearch)  
    AS OccurrenceCount  

count-occurence-of-string-sql-min.png

Above is the sample output.

2
At:- 8/8/2018 4:06:18 PM


profileImage Answered by:- LuneAgile

Thanks so much Vilas.

0
At:- 8/8/2018 5:57:37 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use