Solved

I need to select just a substring of

Posted on 2014-01-15
4
184 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
[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
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

635 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