Solved

how to organize sequence of words by size

Posted on 2013-06-16
2
371 Views
Last Modified: 2013-06-17
hi, lets say i have this var inside one storeprocedure and i want to accomplish this

DECLARE @STR VARCHAR(300) = 'I Love to eat steak in the restaurant'

what i want is to organize the words by size with , between them and not include words that have less then 3 characters.

restaurant, steak, Love, the
0
Comment
Question by:rafaelrgl
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39251509
the solution below needs you to create a user defined function first...
The user defined function is split by a charecter and return a table.

The below is the code for that user defined function split... you can know more about it at the link http://everysolution.wordpress.com/2011/07/28/udf-to-split-a-delimited-string-and-return-it-as-a-table/

CREATE FUNCTION dbo.Split
(
 @RowData nvarchar(2000),
 @SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
 Id int identity(1,1),
 Data nvarchar(100)
)
AS
BEGIN
 Declare @Cnt int
 Set @Cnt = 1
 DECLARE @index INT
 SET @index = Charindex(@SplitOn,@RowData)
 While (@index>0)
 Begin
 Insert Into @RtnValue (data)
  Select
 Data = ltrim(rtrim(Substring(@RowData,1,@index-1)))
 
 Set @RowData = Substring(@RowData,@index+1,len(@RowData))
 Set @Cnt = @Cnt + 1
 SET @index = Charindex(@SplitOn,@RowData)
 End
 
 Insert Into @RtnValue (data)
 Select Data = ltrim(rtrim(@RowData))
 
 Return
END

Open in new window


after creating this function then you can write your code as below

DECLARE @STR VARCHAR(300) = 'I Love to eat steak in the restaurant'
;with CTE AS
(
 select Data,len(Data) as L FRom dbo.split(@STR,' ')
)
select stuff((select ','+Data From CTE where L > 2 for xml path('')),1,1,'')

Open in new window

0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 39253598
Thanks man, your function help me understand and build the right solution, but the points still goes to you, here is what i want:

DECLARE @KEYWORDS VARCHAR(8000) = 'I Love to eat steak in the restaurant'
DECLARE @TB1 TABLE(VWORD NVARCHAR(100))
DECLARE @CNT INT = 1
DECLARE @INDEX INT = CHARINDEX(' ',@KEYWORDS)
While (@index>0)
Begin
     Insert Into @TB1
     Select ltrim(rtrim(Substring(@KEYWORDS,1,@index-1)))
 
     Set @KEYWORDS = Substring(@KEYWORDS,@index+1,len(@KEYWORDS))
     Set @Cnt = @Cnt + 1
     SET @index = Charindex(' ',@KEYWORDS)
End
Insert Into @TB1
Select ltrim(rtrim(@KEYWORDS))
DECLARE @RESULT VARCHAR(2000)
SELECT @RESULT = COALESCE(@RESULT + ', ', '') + VWORD FROM @TB1 WHERE LEN(VWORD) > 3 ORDER BY LEN(VWORD) DESC
SELECT @RESULT
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now