Solved

Exact match when using LIKE

Posted on 2004-03-22
6
208 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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 to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

920 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

15 Experts available now in Live!

Get 1:1 Help Now