473,473 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 41445
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
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.