我正在尝试计算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 |