Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Counting number of words

Author  Topic 

fengfeng
Yak Posting Veteran

64 Posts

Posted - 2010-03-01 : 14:18:03
I have the following sql:


Select devicelist count (*) as 'a_d'
from history
where state in ('In Process', 'fulfilled Order')
and mail = '20100210'
and code IN ('102', '103')
group by devicelist



This would return in 1 column the name of the devicelist, and then the count of it in the 2nd column,

Sometimes the devidelist name would be abc def ghi, and sometimes its just abc. So it could be one word, or 2 or 3 or however many separated by the spaces. But the sql only counts it as 1. Is there a way to count the number of words and then multiply it by the count>?

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-03-01 : 14:31:23
You could do this

select 1 + len(devicelist) - len(replace(devicelist,' ','')) to get the number of words in devicelist.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

fengfeng
Yak Posting Veteran

64 Posts

Posted - 2010-03-01 : 14:39:34
quote:
Originally posted by jimf

You could do this

select 1 + len(devicelist) - len(replace(devicelist,' ','')) to get the number of words in devicelist.

Jim

Everyday I learn something that somebody else already knew



So if i wanted to multiple by the count i do:

select (1 + len(devicelist) - len(replace(devicelist,' ',''))) * count (*)?

also how can I layer in another select to say if devicelist is blank, then dont count, only count those with populated devicelist? And instead of showing every devicelist name, how can I just show the sum of everything excluding those without a devicelist description?
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2010-03-01 : 15:41:57
There was a very clever fellow who published some functions for this in the Script Libray forum.

UDFs GETWORDCOUNT, GETWORDNUM
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=51289





CODO ERGO SUM
Go to Top of Page
   

- Advertisement -