Finding the number of words in a string using SQL

Database: Sql Server

I have a column called page_text that contains the following value

"I love stackoverflow.com because I can post questions and get answers anytime."

Using SQLQUERY , I want to find the number . So in this line it should return 4. I

I could find something.

+5
source share
3 answers
declare @search varchar(10) = 'I'

select len(replace(PageText, @search, @search + '#')) 
- len(PageText) as Count from YourTable 
+9
source

based on this code: http://www.sql-server-helper.com/functions/count-character.aspx

you create a function:

CREATE FUNCTION [dbo].[ufn_CountSpecificWords] ( @pInput VARCHAR(1000), @pWord VARCHAR(1000) )
RETURNS INT
BEGIN

RETURN (LEN(@pInput) - LEN(REPLACE(@pInput, ' ' + @pWord + ' ', '')))

END
GO

, , , , , "," ".". .

, , .

0
SELECT (LEN(@Text) - LEN(REPLACE(@Text,@SearchString,''))/(Len(@SearchString))
-1
source

All Articles