Wrox Programmer Forums
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 25th, 2003, 08:01 AM
Authorized User
 
Join Date: Jun 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Word Count

Quick question:

If I have a table with one of the columns being the text of a message, what is the easiest way to do a query that returns a couple fields out of the table and the number of words in the message? Is this best done client or server side?
 
Old July 25th, 2003, 08:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I would have said client-side. You are not querying a database, you are programmatically parsing a particular field.

That field may have been returned using a server-side query, but you are processing the field on the client.

If you need to do queries like the one you suggested on the server, then maybe you need to consider how you have normalised your data.

It does look like you just need to do it as a one off though, so your data may be perfectly normalised - I leave that distinction to you :)

Regards
Owain Williams
 
Old July 25th, 2003, 08:26 AM
Authorized User
 
Join Date: Jun 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, thanks for the input. I would prefer to do it in SQL, but I think you are right. I'll do it on the client.

Thanks again for the advice.
 
Old July 25th, 2003, 08:53 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

There's a quick kludge to do stuff like this, depends how bothered you are about the intricacies of what makes up a word. If we assume that each word is separated by a space, but there won't be a space at the end of the final word, then all you need to do is count the number of spaces in the string and add 1.
Code:
SELECT (1 + LEN(Field) - LEN(REPLACE(Field, ' ', ''))) AS WordCount
 
Old July 25th, 2003, 08:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Will then LEN and REPLACE functions work in SQL Server (or other RDBMS's for that matter) or are they Access only functions?

Regards
Owain Williams
 
Old July 25th, 2003, 08:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

I never even thought about Access! LEN and REPLACE are available in SQL Server.
 
Old July 25th, 2003, 09:03 AM
Authorized User
 
Join Date: Jun 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you pgtips. I was trying to think of a way to replace all non space characters and then get the len of the remaining spaces, but yours is much easier.

The only problem I can see is that when people put 2 spaces at the start of a sentence, but it doesn't have to be totally exact.
 
Old July 25th, 2003, 09:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hey, like I said its a kludge :).

There is another problem - empty strings have a word count of 1! You can fiddle around with this stuff to your heart's content, e.g. replace double spaces with one space before doing the count, and do a separate test for empty strings. The point is its an easy way to get an approx word count...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Word search - Count Hits bonekrusher XSLT 5 January 2nd, 2008 10:26 AM
how to count lines in word file arshad mahmood VB How-To 2 June 7th, 2006 09:40 PM
character count of ms-word in asp.net sh.rajkumar ASP.NET 1.0 and 1.1 Professional 0 May 19th, 2006 07:24 AM
Count, sum, count a value, return records CongoGrey Access 1 April 18th, 2005 02:25 PM
Coding for Word Count zadiac8318 JSP Basics 1 February 26th, 2005 07:34 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.