gpt4 book ai didi

sql - 如何在Oracle中计算字符串中的单词数?

转载 作者:行者123 更新时间:2023-12-04 05:19:01 26 4
gpt4 key购买 nike

我正在尝试计算SQL字符串中有多少个单词。

Select  ("Hello To Oracle") from dual;

我想显示字数。在给定的示例中,尽管单词之间可能有多个空格,但将为3个单词。

最佳答案

您可以使用与此类似的内容。这将获取字符串的长度,然后减去空格后减去字符串的长度。然后,将数字加到第一应该给你的单词数:

Select length(yourCol) - length(replace(yourcol, ' ', '')) + 1 NumbofWords
from yourtable

参见 SQL Fiddle with Demo

如果您使用以下数据:
CREATE TABLE yourtable
(yourCol varchar2(15))
;

INSERT ALL
INTO yourtable (yourCol)
VALUES ('Hello To Oracle')
INTO yourtable (yourCol)
VALUES ('oneword')
INTO yourtable (yourCol)
VALUES ('two words')
SELECT * FROM dual
;

和查询:
Select yourcol,
length(yourCol) - length(replace(yourcol, ' ', '')) + 1 NumbofWords
from yourtable

结果是:
|         YOURCOL | NUMBOFWORDS |
---------------------------------
| Hello To Oracle | 3 |
| oneword | 1 |
| two words | 2 |

关于sql - 如何在Oracle中计算字符串中的单词数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14008509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com