Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / SQL Server / ADP / October 2006

Tip: Looking for answers? Try searching our database.

UDF for longest word in string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aaron.kempf@gmail.com - 24 Oct 2006 00:01 GMT
Team;

I've got a string like

'hello world grandma'

and I want to find or write a UDF that will tell me the longest WORD
out of that string (in other words, grandma).. i'm using SQL 2000 of
course :)

-Aaron
Russ Rose - 24 Oct 2006 06:19 GMT
Looping through the input string either one character at a time or jumping
from space to space using charindex would be the common way to go.

I would be interested in creating a Split function that built a temp table
instead of an array. Then you could use the LEN function to easily find
and/or sort the individual words.

Usage of below function:

SELECT * FROM dbo.Split('In the course of human events', ' ')

This could get the longest word
SELECT TOP 1 value FROM dbo.Split('In the course of human events', ' ')
ORDER BY length desc

CREATE   FUNCTION dbo.Split(@str varchar(8000), @delimiter varchar(20) = '
')

RETURNS @T TABLE

( position int IDENTITY PRIMARY KEY,
 value varchar(8000) ,
 length smallint null
)

AS

BEGIN
DECLARE @i int
SET @i = -1
WHILE (LEN(@str) > 0)
  BEGIN
    SET @i = CHARINDEX(@delimiter , @str)
    IF (@i = 0) AND (LEN(@str) > 0)
      BEGIN
        INSERT INTO @T (value, length) VALUES (@str, LEN(@str))
          BREAK
      END
    IF (@i > 1)
      BEGIN
        INSERT INTO @T (value, length) VALUES (LEFT(@str, @i - 1),
LEN(LEFT(@str, @i - 1)))
        SET @str = RIGHT(@str, (LEN(@str) - @i))
      END
    ELSE
      SET @str = RIGHT(@str, (LEN(@str) - @i))
    END
RETURN
END

> Team;
>
[quoted text clipped - 7 lines]
>
> -Aaron
aaron.kempf@gmail.com - 24 Oct 2006 14:07 GMT
yeah I ended up using it in vb; but i'll definitely check that out
thanks!

-Aaron

> Looping through the input string either one character at a time or jumping
> from space to space using charindex would be the common way to go.
[quoted text clipped - 57 lines]
> >
> > -Aaron
Alex Kuznetsov - 24 Oct 2006 19:40 GMT
Aaron,

There is a simple set based solution:

DECLARE @c VARCHAR(70)
SET @c = ' 123 12345 1234567 123456 1234567 123 1234 '
SELECT TOP 1 SUBSTRING(@c, NotBlank, NextBlank)
FROM (
SELECT n.Number NotBlank, CHARINDEX(' ', @c, n.Number + 1) - n.Number
NextBlank FROM dbo.Numbers n
WHERE SUBSTRING(@c, n.Number, 1) <> ' '
) t
ORDER BY NextBlank DESC

----------------------------------------------------------------------
1234567

(1 row(s) affected)

-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/
aaron.kempf@gmail.com - 24 Oct 2006 19:58 GMT
yeah I love those recursive way to do things like that.. it seems
pretty darn powerful

now.. i dont want to sound dumb.. but by 'set based' this means it's
not cursor based.. right?

also; is this 2005 specific?

-Aaron

> Aaron,
>
[quoted text clipped - 19 lines]
> http://sqlserver-tips.blogspot.com/
> http://sqlserver-puzzles.blogspot.com/
Alex Kuznetsov - 24 Oct 2006 20:16 GMT
I tested against SQL 2000, works all right. BTW this is not a recursive
query.

-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.