473,383 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Using CHARINDEX vs LIKE in WHERE?

Hi All,

Just wondering if it's any more efficient to use the following SQL
statement to return a partial match from a column as opposed to the
second statement.

SELECT * FROM Table1 WHERE CHARINDEX('value', mycol) > 0
Versus:

SELECT * FROM Table1 WHERE mycol LIKE '%value%'

Does anyone have any thoughts on whether or not either of these
statements would return a result quicker than the other?

Many thanks in advance!

Much warmth,

Murray
Jul 20 '05 #1
5 41415
I think they are probably nearly identical. Neither can use an index. Any
way you could narrow down the query a bit so it might be able to use an
index?
Regards,
Chuck Conover
www.TechnicalVideos.net

"M Wells" <pl**********@planetthoughtful.org> wrote in message
news:80********************************@4ax.com...
Hi All,

Just wondering if it's any more efficient to use the following SQL
statement to return a partial match from a column as opposed to the
second statement.

SELECT * FROM Table1 WHERE CHARINDEX('value', mycol) > 0
Versus:

SELECT * FROM Table1 WHERE mycol LIKE '%value%'

Does anyone have any thoughts on whether or not either of these
statements would return a result quicker than the other?

Many thanks in advance!

Much warmth,

Murray

Jul 20 '05 #2
Just to add to what Chuck said, a LIKE expression that begins with a constant
such as SELECT * FROM Table1 WHERE mycol LIKE 'value%' -can- make use of an
index, so it's probably best to get in the habit of using LIKE rather than
CHARINDEX so as to get the benefit of indexes in those cases when that does
work.

On Sat, 6 Mar 2004 15:08:37 -0700, "Chuck Conover" <cc******@commspeed.net>
wrote:
I think they are probably nearly identical. Neither can use an index. Any
way you could narrow down the query a bit so it might be able to use an
index?
Regards,
Chuck Conover
www.TechnicalVideos.net

"M Wells" <pl**********@planetthoughtful.org> wrote in message
news:80********************************@4ax.com.. .
Hi All,

Just wondering if it's any more efficient to use the following SQL
statement to return a partial match from a column as opposed to the
second statement.

SELECT * FROM Table1 WHERE CHARINDEX('value', mycol) > 0
Versus:

SELECT * FROM Table1 WHERE mycol LIKE '%value%'

Does anyone have any thoughts on whether or not either of these
statements would return a result quicker than the other?

Many thanks in advance!

Much warmth,

Murray


Jul 20 '05 #3
Steve Jorgensen (no****@nospam.nospam) writes:
Just to add to what Chuck said, a LIKE expression that begins with a
constant such as SELECT * FROM Table1 WHERE mycol LIKE 'value%' -can-
make use of an index, so it's probably best to get in the habit of using
LIKE rather than CHARINDEX so as to get the benefit of indexes in those
cases when that does work.


And to add even more to what Steve and Chuck says, it is not really
true that LIKE '%val%' or charindex('value', col) cannot use an index.
What is true is that SQL Server cannot *seek* an index, that is looking
up values through the index structure. However, it can still scan the
index, and find matches this way. For a SELECT * type of query this is
not a very likely strategy, because it could lead to too many data-page
retrievals. However for the query

SELECT mycol FROM Table1 WHERE mycol LIKE '%value%'

SQL Server would use the index, since the query is covered completely
by the index.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
M Wells (pl**********@planetthoughtful.org) writes:
Just wondering if it's any more efficient to use the following SQL
statement to return a partial match from a column as opposed to the
second statement.

SELECT * FROM Table1 WHERE CHARINDEX('value', mycol) > 0

Versus:

SELECT * FROM Table1 WHERE mycol LIKE '%value%'

Does anyone have any thoughts on whether or not either of these
statements would return a result quicker than the other?


My vote goes for LIKE, although the support I have for this suggestion
is not solid.

Last year I ran a couple of tests to benchmark different methods to find
data using a comma-separated lists as input. There was one method using
charindex and another using LIKE. Both had absymal performance, compared
to the other method. Interestingly enough, for one of the tests LIKE
was four times faster than charindex, but only one of the three machines
on which I ran the test. On the other two machines, the timings were
very close to each other.

All this may seem very strange, until I tell you that the two machines
with identical performance were single-processor machine, whereas the
machine where LIKE was four times faster has four CPUs.

So if you are using a multi-processor, LIKE may be a better bet, if
SQL Server is better at using parallelism for LIKE. But the only way
to find is to benchmark for *your* query.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5
I think a little advantage will be done on CHARINDEX search, because the
algorithm is done for a single string without joker chars while the use
of LIKE as been created for every char strings wich includes empty ones
as well as very wides ones with jokers...

A +

M Wells a écrit:
Hi All,

Just wondering if it's any more efficient to use the following SQL
statement to return a partial match from a column as opposed to the
second statement.

SELECT * FROM Table1 WHERE CHARINDEX('value', mycol) > 0
Versus:

SELECT * FROM Table1 WHERE mycol LIKE '%value%'

Does anyone have any thoughts on whether or not either of these
statements would return a result quicker than the other?

Many thanks in advance!

Much warmth,

Murray


--
Frédéric BROUARD, MVP Microsoft SQL Server. Langage SQL / Delphi / web
Livre SQL - col. Référence : http://sqlpro.developpez.com/bookSQL.html
Le site du SQL, pour débutants et pros : http://sqlpro.developpez.com
****************** mailto:br******@club-internet.fr ******************

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Samuel Walters | last post by:
I'm currently considering a position teaching math and possibly programming at a small local magnet school. If I was also teaching programming, I would, of course, be using Python. So, I went...
3
by: Anders Borum | last post by:
Hello! I've come across a strange error that occurs, when you try to return a nodelist from a variable with a choose/where/otherwise statement. I'm not quite sure whether it's a bug or simply...
2
by: coolvixs | last post by:
hi, i have some data in mysql in a column like this: 6.1.01 Financial Regulations 3.32.02 Academic Counseling what iam selecting is select by letter A, B, C...Z. so my first char in the...
2
by: matthewwhaley | last post by:
What is the best way to essentially use the charindex(find) function if the value is could be more than one variable (A or B or C) I can't seem to get an "or", "if" or "select if" to work ...
11
by: Angus | last post by:
I am working with a C API which often requires a char* or char buffer. If a C function returns a char* I can't use string? Or can I? I realise I can pass a char* using c_str() but what about...
6
by: Jared | last post by:
Consider the following two functionally identical example queries: Query 1: DECLARE @Name VARCHAR(32) SET @Name = 'Bob' SELECT * FROM Employees WHERE = CASE WHEN @Name IS NULL THEN ELSE...
5
by: neosam | last post by:
Hey all, I am not able to integrate these 2 actions in one command. What i am doing is... A button which sends a report with only specific data to a particular person (via outlook). I am able to...
3
by: jmckown | last post by:
I'm attempting to pull a list of Google search terms from url's stored in a database. Here is an example of the data in the table I'm dealing with http://www.google.com/search?hl=en&q=proxy...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.