Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

I need to select just a substring of

Posted on 2014-01-15
4
Medium Priority
?
191 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 800 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 1200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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…
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

879 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