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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Counting words in text field

Author  Topic 

ProEdge
Yak Posting Veteran

64 Posts

Posted - 2006-05-25 : 12:52:21
Does anyone know of a way of counting the number of words in a text field using SQL? Thanks

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-05-25 : 13:15:06
How do u define "Words" ?
Any set of character(s) seperated by spaces ?
How about punctuations brackets etc ?

Once u define the "Word",
U may be able to find a function (if u dig this forum)

Srinika
Go to Top of Page

ProEdge
Yak Posting Veteran

64 Posts

Posted - 2006-05-25 : 14:00:30
Yes by words I do mean a set of character(s) separated by spaces. Example: Batman Part II
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-05-25 : 14:58:27
quote:
Originally posted by ProEdge

Example: Batman Part II


How many words in it 2 or 3

how many words in the following :
1. www.google.com
2. How many < more > words in this
3. How many words here


Srinika
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-05-25 : 15:51:31
[code]
CREATE FUNCTION udf_Word_Count
(@str varchar(8000), @word varchar(255))
RETURNS int
AS
BEGIN
DECLARE @DelimFound int, @LastDelimPosition int, @Count int
SELECT @DelimFound = 0, @LastDelimPosition = 0, @Count = 0

WHILE (@DelimFound <= LEN(@str))
BEGIN
IF (CHARINDEX(@Word, @str, @LastDelimPosition + 1) = 0)
BREAK

ELSE
BEGIN
SET @Count = @Count + 1
SET @LastDelimPosition = CHARINDEX(@Word, @str, @LastDelimPosition + 1)
END

END

RETURN @Count

END
[/code]


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

ProEdge
Yak Posting Veteran

64 Posts

Posted - 2006-05-25 : 15:55:19
X002548, thanks for that. I'll try it out and if I have problems, I'll let you know.
Go to Top of Page

sshelper
Posting Yak Master

216 Posts

Posted - 2006-05-25 : 16:31:05
You may also want to look at the user-defined function in the following link:

http://www.sql-server-helper.com/functions/count-words.aspx

SQL Server Helper
http://www.sql-server-helper.com
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-05-25 : 19:10:06
I that clever Igor fellow posted a function for this in the Script Library Forum.

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



CODO ERGO SUM
Go to Top of Page
   

- Advertisement -