Solved

how to organize sequence of words by size

Posted on 2013-06-16
2
374 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

786 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