簡體   English   中英

查找字符串中的單詞數

[英]Find the count of words in string

SQL:如何在以下示例中找到單詞數?

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

子問題:

  1. 如何計算空格?
  2. 如何將雙倍/三倍/...空格算作一個? Gordon Linoff 在這里回答

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

  4. 沒有string_split函數是否可能(因為它僅自 SQL SERVER 2016 起可用)?

最好的解決方案總結在這里

感謝Gordon Linoff 在這里的回答

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

一種方法使用遞歸 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
;

是一個 db<>fiddle。

第一件事是你需要刪除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,' ',']['), '[]', ''), '][', ' '), ' ')

僅用於字數統計並且如果您的 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]%'

使用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 0);

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

作為內聯函數:

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 0); -- 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 0); -- don't work within inline function
);

go

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM