Solved

Exact match when using LIKE

Posted on 2004-03-22
6
206 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
go with charindex perhaps...

do you also need to consider

'.The '
or ',the '

if punctuation is being used

?
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 41
Backup Job question 4 18
SQL Server 2012 Row Selection 2 29
Test a query 23 11
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…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 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

19 Experts available now in Live!

Get 1:1 Help Now