Search count of words within a string using SQL

前端 未结 3 1795
悲&欢浪女
悲&欢浪女 2020-12-19 10:24

Database: Sql Server

I have table column named page_text which contains following value

\" I Love stackoverflow.com because i can post questio

相关标签:
3条回答
  • 2020-12-19 10:58
    declare @search varchar(10) = 'I'
    
    select len(replace(PageText, @search, @search + '#')) 
    - len(PageText) as Count from YourTable 
    
    0 讨论(0)
  • 2020-12-19 11:10
    SELECT (LEN(@Text) - LEN(REPLACE(@Text,@SearchString,''))/(Len(@SearchString))
    
    0 讨论(0)
  • 2020-12-19 11:16

    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
    

    this however implies that you save your strings with a leading and trailing space and you replace any other separators like ',' and '.' with spaces.

    and you need to refrase your question if you want the count of words or just the appeareance of a word.

    0 讨论(0)
提交回复
热议问题