Solved

Exact match when using LIKE

Posted on 2004-03-22
6
212 Views
Last Modified: 2009-08-03
I am using MS SQL 2000 and would like to get the exact text values when doing a search. However when i use a LIKE it brings back more than i need.

For example some rows contain this and i want just 'the'

then i did this
the best one
there we go
testthe water
the there then
then there the

This is the query i have come up with, is this the only way of doing it where i look for the combinations for the start, middle and end taking into account spaces. Seems very long winded.

select * from test
where test like 'the %' or test like '% the' or test like '% the %'

Thanks
0
Comment
Question by:zzub
[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
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
Sven earned 125 total points
ID: 10648513
try this:
select * from test where CHARINDEX(test, ' the ') > 0

CHARINDEX(string to search in, string to search for, starting position in string)
returns position of string.
0
 
LVL 13

Expert Comment

by:danblake
ID: 10648524
sql-server does not have brilliant text string matching services.
The approach you have is the best sql-server offers at the moment.

select * from test
where test like 'the %' or test like '% the ' or test like '% the %'

the additional space in the second set '% the' to '% the ' should make all the difference.
0
 
LVL 13

Expert Comment

by:danblake
ID: 10648533
Using the sql-given (modified)

This will eliminate/include the following:
then i did this -- eliminated from list
the best one -- included in list
there we go  -- eliminated from list
testthe water -- eliminated from list
the there then -- included in list
then there the -- included in list

0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 125 total points
ID: 10649616
go with charindex perhaps...

do you also need to consider

'.The '
or ',the '

if punctuation is being used

?
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
efficient backup report for SQL Server 13 79
SQL XPCMDSHELL SQLCMD 1 39
SQL Server 2012 to SQL Server 2016 24 54
sql trace 4 27
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

734 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