Solved

I need to select just a substring of

Posted on 2014-01-15
4
172 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 31

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now