查找字符串中的单词数

问题描述 投票:0回答:5

SQL:如何在下面的示例中查找单词数?

declare @s varchar(55) = 'How to find the  count  of words in this string ?'

子问题:

  1. 如何计算空格?
  2. 如何将双倍/三倍/ ...个空格计算为一个? answer by Gordon Linoff here

  3. 如何避免计算特殊字符?例如:'Please , don't count this comma'

  4. 没有string_split功能是否有可能(因为它仅自SQL SERVER 2016起可用?

最佳解决方案摘要HERE

sql sql-server string count word
5个回答
1
投票

一种方法使用递归CTE:

declare @s varchar(55) = 'How to find the  count   of words in this string ?';


with cte as (
      select convert(varchar(max), '') as word, 
             convert(varchar(max), ltrim(@s)) as rest
      union all
      select left(rest, patindex('%[ ]%', rest + ' ') - 1),
             ltrim(stuff(rest, 1, patindex('%[ ]%', rest + ' '), ''))
      from cte
      where rest <> '' 
     )
select count(*)
from cte
where word not in ('', '?', ',')
--OPTION (MAXRECURSION 1000);   -- use if number of words >99
;

Here是db <>小提琴。


2
投票

感谢Gordon Linoff的回答here

SELECT len(replace(replace(replace(replace(@s,' ','<>'),'><',''),'<>',' '),' ',','))

OutPut
-------
How,to,find,the,count,of,words,in,this,string?

SELECT replace(replace(replace(replace(replace(@s,' ','<>'),'><',''),'<>',' '),' ',','),',','')

OutPut
------
Howtofindthecountofwordsinthisstring?

现在您可以找到两个输出的长度之间的差,并为最后一个单词加1,如下所示。

declare @s varchar(55) = 'How to find the  count  of words in this string?'

SELECT len(replace(replace(replace(replace(@s,' ','<>'),'><',''),'<>',' '),' ',',')) 
-len(replace(replace(replace(replace(replace(@s,' ','<>'),'><',''),'<>',' '),' ',','),',',''))
+ 1 AS WORD_COUNT

WORD_COUNT
----------
10

http://sqlfiddle.com/#!18/06c1d/5


1
投票

第一件事是您需要将double/tripple..或更多计数减为一个。

declare @str varchar(500) = 'dvdv sdd      dfxdfd dfd'

select Replace(Replace(Replace( @str,' ',']['), '[]', ''), '][', ' ')

这将删除单词之间的所有不必要的space,您将得到最终的单词。

[之后,您可以使用string_split(对于SQL SERVER 2016及更高版本)。要计算文本中的单词数,其中minus 1是您的总空格数。

select count(value) - 1 from string_split( @str, ' ')

最终查询看起来像

declare @str varchar(500) = 'dvdv sdd      dfxdfd dfd'

select count(value) - 1 from string_split( Replace(Replace(Replace( @str,' ',']['), '[]', ''), '][', ' '), ' ')


1
投票

仅用于字数统计,并且如果您的MSSQL版本支持STRING_SPLIT,则可以在下面使用此简单脚本-

DECLARE @s VARCHAR(55) = 'How to find the  count  of words in this string ?'

SELECT 
COUNT(
    IIF(
        LTRIM(value)='',
        NULL,
        1
     )
) 
FROM STRING_SPLIT(@s, ' ')
WHERE value LIKE '%[0-9,A-z]%'

0
投票

使用string_split(仅自SQL SERVER 2016起可用:]

declare @string varchar(55) = 'How to find the  count  of words in this string ?';

select count(*) WordCount from string_split(@string,' ') where value like '%[0-9A-Za-z]%'

以下答案使用相同的想法:

不使用string_split

declare @string varchar(55) = 'How to find the  count  of words in this string ?';

;with space as 
    (   -- returns space positions in a string
        select cast(           0                   as int) idx union all
        select cast(charindex(' ', @string, idx+1) as int)     from  space
        where       charindex(' ', @string, idx+1)>0
    )
select count(*) WordCount from space 
where substring(@string,idx+1,charindex(' ',@string+' ',idx+1)-idx-1) like '%[0-9A-Za-z]%'
OPTION (MAXRECURSION 32767);

以下答案使用相同的想法:

作为内联函数:

ALTER FUNCTION dbo.WordCount
(   
    @string        NVARCHAR(MAX) 
,   @WordPattern   NVARCHAR(MAX) = '%[0-9A-Za-z]%'
)
/*  
Call Example:

    1) Word count for single string:

    select * from WordCount(N'How to find the  count  of words in this string ? ', default)


    2) Word count for set of strings:

    select *
     from (
               select 'How to find the  count  of words in this string ? ' as string union all 
               select 'How many words in 2nd example?'
           ) x    
    cross apply WordCount(x.string, default)

Limitations:
    If string contains >100 spaces function fails with error:

    Msg 530, Level 16, State 1, Line 45
    The statement terminated. The maximum recursion 100 has been exhausted before statement completion.

    NB! OPTION (MAXRECURSION 32767); -- don't work within inline function

*/
RETURNS TABLE AS RETURN 
(    
    with space as 
    (   -- returns space positions in a string
        select cast(           0                   as int) idx union all
        select cast(charindex(' ', @string, idx+1) as int)     from  space
        where       charindex(' ', @string, idx+1)>0
    )
    select count(*) WordCount from space 
    where substring(@string,idx+1,charindex(' ',@string+' ',idx+1)-idx-1) like @WordPattern
    --    OPTION (MAXRECURSION 32767); -- don't work within inline function
);

go
© www.soinside.com 2019 - 2024. All rights reserved.