【问题标题】:SQL String: Counting Words inside a StringSQL 字符串:计算字符串中的字数
【发布时间】:2017-01-31 07:48:48
【问题描述】:

我在这里搜索了许多问题,但我找到的所有问题都是针对不同的语言,如 Javascript 等。

我在 SQL 中有一个简单的任务,但我似乎找不到简单的方法来完成。 我只需要计算 SQL 字符串(一个句子)中“单词”的数量。您可以在我的示例中看到为什么“单词”在引号中。 “单词”由空格分隔。

例句:

1. I am not your father.
2. Where are your brother,sister,mother?
3. Where are your brother, sister and mother?
4. Who are     you?

想要的答案:

1. 5
2. 4
3. 7
4. 3

如您所见,我需要计算“单词”而不考虑符号(我必须将它们视为单词的一部分)。所以在样品编号中。 2: (1)Where (2)are (3)your (4)brother,sister,mother? = 4

我可以通过这样的替换来处理多个空格:
REPLACE(string, ' ', ' ') -> 2 whitespaces to 1 REPLACE(string, ' ', ' ') -> 3 whitespaces to 1 and so on..

我可以使用什么 SQL 函数来执行此操作?我使用的是 SQL Server 2012,但需要一个也适用于 SQL Server 2008 的函数。

【问题讨论】:

  • 如果你问我,这是一个棘手的问题。例如,假设您将数字 3,000 作为单词之一出现。那么,在这种情况下,用逗号分割是不正确的。你应该清楚地告诉我们这里的规则是什么。
  • @TimBiegeleisen 逗号或任何其他特殊字符将被视为“单词”的一部分。在我的问题中说明了这一点。它将满足的唯一有效分隔符是单个空格。
  • 必须在 SQL 中完成吗? :|
  • @super-user 您可以使用 SQLCLR 编写适当的函数或使用 Regexp 快速计算单词 WITHOUT 生成临时字符串。每个REPLACE 都会生成字符串,也可能导致错误的执行计划。
  • 对于 SQL Server 2008 - 它不再受支持。 2012是最早的版本。 2016 提供STRING_SPLIT 功能

标签: sql-server string


【解决方案1】:

这是一种方法:

创建并填充示例表(在您以后的问题中保存此步骤)

DECLARE @T AS TABLE
(
    id int identity(1,1),
    string varchar(100)
)

INSERT INTO @T VALUES
('I am not your father.'),
('Where are your brother,sister,mother?'),
('Where are your brother, sister and mother?'),
('Who are     you?')

使用cte将多个空格替换为单个空格(感谢Gordon Linoff的回答here

;WITH CTE AS
(
SELECT  Id,
        REPLACE(REPLACE(REPLACE(string, ' ', '><' -- Note that there are 2 spaces here
                               ), '<>', ''
                       ), '><', ' '
                ) as string
FROM @T
)

查询CTE-字符串长度-不带空格的字符串长度+1:

SELECT id, LEN(string) - LEN(REPLACE(string, ' ', '')) + 1 as CountWords
FROM CTE 

结果:

id  CountWords
1   5
2   4
3   7
4   3

【讨论】:

  • 这正是我要找的。做得很好而且很简单。请问为什么需要 + 1 才能使其正常工作?
  • 因为[字符串长度] - [不带分隔符的字符串长度] 返回的是分隔符的个数。要获取值的数量,您需要添加一个。
  • @ZoharPeled 这是一个很好的答案。但是它会将空字符串计为一个单词
【解决方案2】:

这是@ZoharPeled 回答的一个小改进。这也可以处理 0 长度值:

DECLARE @t AS TABLE(id int identity(1,1), string varchar(100))

INSERT INTO @t VALUES
  ('I am not your father.'),
  ('Where are your brother,sister,mother?'),
  ('Where are your brother, sister and mother?'),
  ('Who are     you?'),
  ('')

;WITH CTE AS
(
  SELECT
    Id,
    REPLACE(REPLACE(string,' ', '><'), '<>', '') string
  FROM @t
)
SELECT 
  id,
  LEN(' '+string)-LEN(REPLACE(string, '><', ' ')) CountWords
FROM CTE

【讨论】:

    【解决方案3】:

    要处理多个空格,请使用此处显示的方法

    Declare @s varchar(100)
    set @s='Who are     you?'
    set @s=ltrim(rtrim(@s))
    
    while charindex('  ',@s)&gt;0
    Begin
        set @s=replace(@s,'  ',' ')
    end
    
    select len(@s)-len(replace(@s,' ',''))+1 as word_count
    

    https://exploresql.com/2018/07/31/how-to-count-number-of-words-in-a-sentence/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-14
      • 2023-04-06
      • 2018-04-20
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      相关资源
      最近更新 更多