Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search count of words within a string using SQL

Database: Sql Server

I have table column named page_text which contains following value

" I Love stackoverflow.com because i can post questions and get answers at any time. "

Using SQLQUERY i want to search the number of I it has . So in this string it would should return 4.

I should be able to search anything.

like image 517
Chief Avatar asked Oct 14 '11 11:10

Chief


People also ask

How do I count the number of words in a string in SQL?

Steps to solve: Add a column to your table and index it: ALTER TABLE tablename ADD COLUMN wordcount INT UNSIGNED NULL, ADD INDEX idxtablename_count (wordcount ASC); . Before doing your INSERT, count the number of words using your application.

How do I count data in a string in SQL?

SQL Server LEN() Function The LEN() function returns the length of a string. Note: Trailing spaces at the end of the string is not included when calculating the length. However, leading spaces at the start of the string is included when calculating the length.

How do you find a count of a specific character of a string in SQL?

LEN() Function in SQL Server LEN() function calculates the number of characters of an input string, excluding the trailing spaces. It is an expression that can be a constant, variable, or column of either character or binary data.

How do I search for a word in SQL query?

We can use LIKE Operator of SQL to search sub-string. The LIKE operator is used with the WHERE Clause to search a pattern in string of column. The LIKE operator is used in a conjunction with the two wildcards characters.


1 Answers

declare @search varchar(10) = 'I'

select len(replace(PageText, @search, @search + '#')) 
- len(PageText) as Count from YourTable 
like image 109
t-clausen.dk Avatar answered Sep 22 '22 04:09

t-clausen.dk