Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RETURN shorter word from WORD COUNT 2

Status
Not open for further replies.

Out2work

Technical User
Oct 7, 2009
12
GB
Hi , I need a function where i pass it a description and get a truncated description if the word count = 30

can someone please help me with this
 
Not fully tested but this seems to return a complete word at the end of set length which helps

--Code
select top 1 Left([description],(121-Charindex(' ',Reverse(left([description],120)))))from Table
 
Not sure what you want.
Could you post some example data and what you want from it?

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
THis is for a website

Product descriptions can be long and im using a popup with set length and width to display the item description.

So i want the end of description to say ...(Click to read more)

-----Sample Data of description example:------

"Sample desciption Armoir helps preserve your items with a special lining while it carefully organizes your treasures with cubbies, hooks, drawers, and compartments for everything. Spacious is a fine piece of furniture, crafted from fine wood and wood veneers.

* keeper anti-tarnish lining to keep your silver jewelry from tarnishing for up to 40 years with proper use
* Holds approximately 500 pieces of items
* Specially designed compartments and 10 earring stands
* Tilit-back lid with wood-framed mirror
* Measures approximately 44 x 21 x 12
* Made in China
"

------Output should be:-------

"Sample desciption Armoir helps preserve your items with a special lining while it carefully organizes your treasures with cubbies, hooks, drawers, and compartments for everything. Spacious is a " + ...(Click to read more)

 
Do you always want 30 words or pick some length and return a string within this length but ended with a word (not truncate words)?

 
Yes i would like to force an amount of words 30 in this case for all descriptions
 
And what if the 31th starts word on 1243 char?
(there are languages with very long words, you know)

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
I think we need to split words and create a string as we go, so in this particular case I recommend to start from and modify to return a scalar instead consisting of N number of words.

Usually I recommend to use different functions to splitting words, but in this case this function may be the best to work with.
 
Hi,

If you're using SQL2005+ you can solve this using XML PATH for.

First create a SPLIT function
Code:
CREATE FUNCTION Split
(
  @delimited nvarchar(max),
  @delimiter nvarchar(20)
) RETURNS @t TABLE
(
  val nvarchar(max)
)
AS
BEGIN
  declare @xml xml
  set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'

  insert into @t(val)
  select 
    r.value('.','varchar(5)') as item
  from @xml.nodes('//root/r') as records(r)

  RETURN
END

Test it
Code:
select val 
from dbo.split('one two three four    five',' ')
where val <> ''

Notice all the empty characters are removed? You can then use XML path again to rebuild your string. In this case i'm grabbing the first 3 words.
Code:
select top 1 replace(
(select top 3 val as 'data()'         
 from (
  select val 
  from dbo.split('one two three four    five',' ')
  where val <> ''
 ) as t1
for xml path('')),' ',' ')

Result should be
Code:
one two three

Ryan
 
IMHO, we're doing two steps operation - split first and then concatenate back. I was thinking it's better to modify the split function to do both operations at once.

I'll post a function version to get N words from a string later on.
 
I just slightly modified the function I gave a reference earlier (didn't test it much) - it would be nice if you can compare it with Ryan's approach in terms of speed/performance:

Code:
ALTER FUNCTION GetShorterString 
	(@STR VARCHAR(8000),
	 @separator VARCHAR(16)=' ',
	 @NumOfWords int = 30) 
RETURNS varchar(8000)
AS
/* Splits passed string into items based on the specified separator string
 Parameters:
	@str  - The string to split
	@separator - The separator string ( space is default)
	@NumOfWords - number of items to retrieve
 Returns string 
*/
 
BEGIN
	DECLARE @Item VARCHAR(128), @pos INT, @FinalResult varchar(8000), @ItemNumber int
	set @FinalResult = ''
	set @ItemNumber = 0
	WHILE DATALENGTH(@STR) > 0 AND @ItemNumber < @NumOfWords
	BEGIN
		SET @pos = CHARINDEX(@separator, @STR)
		IF @pos = 0 		    
			SET @pos = DATALENGTH(@STR)+1
        set @ItemNumber = @ItemNumber + 1
		SET @Item = LEFT(@STR, @pos -1 )
		SET @STR = SUBSTRING(@STR, @pos + DATALENGTH(@separator), 8000)
		set @FinalResult = @FinalResult + @separator + @Item
	END
 
	RETURN substring(@FinalResult,2,len(@FinalResult))
 
END
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top