How do I count the number of times a specific word appears in a text type field

Paula Houst 1 Reputation point
2022-12-19T19:36:11.247+00:00

I've created a variable with the word that I'm searching for and used the replace command to replace any instances of this word with ''. However, it errors as the field where I'm applying the replace function is a text type field. How do I get around this.

DECLARE @one nvarchar(max) = 'PASSED'

SELECT
(LEN(itemnote.notes) - LEN(replace(itemnote.notes,@one ,'')))/LEN(@one ) AS passes
FROM itemnote.notes

SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,061 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-12-20T05:47:00.373+00:00

    Hi @Paula Houst
    Are you trying to implement this statement in SSMS?
    Since you added the SSRS tag, I can provide you with an expression in SSRS to count the number of occurrences of a specific word in a text type field.

    =(LEN(Fields!QuickFox.Value) - LEN(REPLACE(Fields!QuickFox.Value, Parameters!CharToSearch.Value,"")))/Len(Parameters!CharToSearch.Value)  
    

    272258-1.png
    As for your statement, I tested it in SSMS and it can work. I don't know what your specific problem is, maybe you can seek help under the T-sql tag: sql-server-transact-sql.html.
    Best regards,
    Aniya

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.