A union of curiosity and data science

Knowledgebase and brain dump of a database engineer


T-SQL Word Count Function

 

 
 
Function

create function f_count_word_in_string (@field varchar(max), @word varchar(max))

returns int
as
begin

   return (LEN(@field) - LEN(REPLACE(@field, @word,'')))/LEN(@word)
end
 
 
 

 Test:

select dbo.f_count_word_in_string('tester

I want to test this, but how?  

oh tester tester', 'tester')

 
 Result: