I was wondering, how can I write a query that counts the number of words in a sentence, not including one-word special characters / punctuations?
for example:
sentence | numofwords |
---|---|
'Hello world, how are you?' | 5 |
'Me & my friend are here' | 5 |
'I think it's great' | 4 |
'Who # is # that?' | 3 |
'Oh , didn't see you there!' | 5 |
I got to this point:
with cte as(
select sentence,
replace(sentence, '&', '') as nosymbolname
from database
)
select sentence,
(length(nosymbolname) - length(replace(nosymbolname, ' ', ''))) + 1 wordsinname
from cte
but it does not apply for cases that have one-word special characters/ punctuations, and I can't really think of a way to address those scenarios.
Thanks!