473,397 Members | 2,056 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,397 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 41430
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.