Solved

I need to select just a substring of

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

752 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