Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to organize sequence of words by size

Posted on 2013-06-16
2
Medium Priority
?
379 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 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

721 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