Solved

I need to select just a substring of

Posted on 2014-01-15
4
177 Views
Last Modified: 2014-01-15
I need to SET this sting
'LIKE ''%'' '
to '% (there will be a variable number of letters here) %'

So basically
IF string IS NOT NULL SET 'LIKE ''%'' ' = .....
I need to remove 'LIKE'    AND Make sure that there is another % and end with '
0
Comment
Question by:portlight
4 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 200 total points
ID: 39783031
Option 1
SELECT * from table where something LIKE '%[%]'

Option2
SELECT * from table where something LIKE '%\%%' ESCAPE '\
0
 
LVL 9

Accepted Solution

by:
Valliappan AN earned 300 total points
ID: 39783032
DECLARE @sql NVARCHAR(max)
DECLARE @yoursearchstring NVARCHAR(max)

SET @yoursearchstring = 'test'  --change it to your search string, or make it a parameter

SET @sql = 'SELECT * FROM [yourtablename] WHERE 1=1 '

IF  @yoursearchstring != ''
BEGIN
   SET @sql = @sql + ' AND [yourcolumn] LIKE ''%' + @yoursearchstring + '%'' '
END

exec sp_executesql @sql

Open in new window

Hope this helps.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39783099
>>'% (there will be a variable number of letters here) %'<<
I sense that the letters will not be randomly variable since anything such as "Like '%'" or "Like '%%'" of "Like '%%%'" will return everything anyway. I suspect you may be trying to find records Like '%[some string value]%' dynamically. Can you show us how you intend to use this string?
0
 

Author Comment

by:portlight
ID: 39783776
This gave me exactly what i was looking for each time.

IF @searchString IS NOT NULL SET @searchString =  SUBSTRING (@searchString , CHARINDEX ( ' ', @searchString ) + 1, LEN (@searchString ))
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

685 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