SQL Server - calculate number of word occurrences in a column, per record

Asked

Viewed 413 times

0

My goal is to create a select to return the total occurrences of a word, a column, per record

My table is modeled as follows: id / filename / qntd_pg / content

ex: I want to search how many times the word 'Rio' appears in the column [content], for each bank record.

The code below gives no error, but returns the amount as Null. It’s the first time I’m using the if structure in Sql Server.

Follows the Code:

declare @palavra varchar(30),
        @cont int,
        @result int
select
    [DCM_conteudo]  
FROM [dcm_digital].[dbo].[conteudo_dcm] 
where upper([DCM_conteudo]) like '%Rio%' and [DCM_id] = 1
    if  @palavra = 'Rio' 
    begin
        select @result = count(@cont);
    end;    
    select @result;

Updating...

Solution in the post: SQL Server - Return only the searched word

2 answers

1

Renata, I made some adjustments to use locally. You will need to change the table used, but this is simple. You can use it as follows:

DECLARE @conteudo_dcm TABLE (DCM_id INT IDENTITY(1,1), DCM_conteudo VARCHAR(100))

INSERT INTO @conteudo_dcm VALUES ('Rio e a RIO vai se repetir então teremos 2 :D')
INSERT INTO @conteudo_dcm VALUES ('um Rio apenas... então teremos 1 :D')
INSERT INTO @conteudo_dcm VALUES ('tres pra este caso. Rio, Rio, Rio XD.. então 3.. blz?')

declare @palavra varchar(30) = 'Rio',
        @cont int,
        @result int,
        @palavraLike varchar(30) = '%Rio%'
SELECT
    [DCM_conteudo],
    (LEN([DCM_conteudo]) - LEN(REPLACE([DCM_conteudo], @palavra, ''))) /
            LEN(@palavra)
FROM @conteudo_dcm
where upper([DCM_conteudo]) like @palavraLike-- and [DCM_id] = 1


In this case I am counting as I locate the word in the string for each line.

  • Thank you for your help, but I’m still at an impasse. Your Cód. returns not only the searched words, but also words that have this word in their composition. Ex: the word is Rio, but also finds the word salary.

  • Well observed! On the link https://stackoverflow.com/questions/6003240/cannot-use-a-contains-or-freetext-predicate-on-table-indexed-view-because-it. I believe we have achieved what you want. In this case you will need to use the Full-text index and use the CONTAINS or FREETEXT function as examples.

0

Try if you only want to count the amount of records that have the word 'Rio' ever, that is, when there are 2 times in my Row will count only one:

declare @palavra varchar(30),
        @result int

select
    @result = Count([DCM_conteudo]) -- Set a variavel Result com a quantidade de registros retornados
    FROM [dcm_digital].[dbo].[conteudo_dcm] 
    where upper([DCM_conteudo]) like '%'+@palavra+'%' -- se necessario and [DCM_id] = 1

select @result; -- Apresenta o resultado
  • 1

    Thank you for the collaboration, Marcos, but I believe that this implementation is not appropriate for the issue since it accounts for the whole and not the specific record. Anyway I’m sure it will help someone with a similar doubt. Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.