Solved

how to organize sequence of words by size

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL STANDARD CORE 7 31
SQL Pivot with row total 5 27
SSAS Store Forecasting data in the cube 1 17
query optimization 6 14
In this article I will describe the Copy Database Wizard 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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

856 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