Die Länge des Strings minus die Länge desselben Strings ohne Leerzeichen ergibt die Anzahl der Leerzeichen. Eins hinzugerechnet, fertig ist die Wortzahl:
Select length(yourCol) - length(replace(yourcol, ' ', '')) + 1 NumbofWords
from yourtableAm Beispiel
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;Die Abfrage liefert:
YOURCOL NUMBOFWORDS
--------------- -----------
Hello To Oracle 3
oneword 1
two words 2Zwei Einschränkungen
Mehrfache Leerzeichen erhöhen das Ergebnis. Bei unsauberen Daten das Eingabefeld vorher mit regexp_replace(yourcol, ' +', ' ') normalisieren.
Und: Ein leerer String ergibt 1 statt 0. Wenn Blanks möglich sind, mit einer NULL- oder Längenprüfung absichern:
SELECT CASE WHEN yourcol IS NULL THEN 0
ELSE length(yourcol) - length(replace(yourcol, ' ', '')) + 1
END AS numbowords
FROM yourtable;