Solved

how to organize sequence of words by size

Posted on 2013-06-16
2
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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…

733 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