我正在尝试计算sql中字符串中有多少单词.
Select ("Hello To Oracle") from dual;
解决方法
你可以使用类似的东西.这将获取字符串的长度,然后在删除空格的情况下减去字符串的长度.然后添加第一个应该给你的字数:
Select length(yourCol) - length(replace(yourcol,' ','')) + 1 NumbofWords from yourtable
如果您使用以下数据:
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 |