473,836 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ 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('valu e', 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 41558
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**********@p lanetthoughtful .org> wrote in message
news:80******** *************** *********@4ax.c om...
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('valu e', 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******@comms peed.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**********@p lanetthoughtful .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('valu e', 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('valu e', 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**********@p lanetthoughtful .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('valu e', 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('valu e', 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
1393
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 out and googled around for resources. I found lots of articles about using Python to teach programming, but I have yet to turn up one forum where teachers using Python talk amongst themselves. Can anyone point me to such a resource, or, at least...
3
2026
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 lack of W3C support. All I know is, that this Xsl document works with the current implementation of MSXML V4.0 SP2 if transformed using e.g. VB6. I'm told by the compiler at runtime, that "the expression should evaluate to a nodeset" (the...
2
9177
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 column is always a integer, how can i select when the first few characters are always a integer, i want to get rid of the integers and run query on the word when selecting.
2
3005
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 Below is the code that works, I need 'GOV' to be either 'GOV' or 'FWD' or 'LIB' sqlstring = "SELECT Distinct substring(," _
11
2718
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 receiving a char buffer into a string? Reason I ask is otherwise I have to guess at what size buffer to create? And then copy buffer to a string. Doesn't seem ideal.
6
5038
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 @Name END Query 2:
5
7604
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 filter the records using the where comand and i am able to send using the DoCmd.SendObject command. but only seperately. Is it possible to insert the where command in the DoCmd.SendObject syntax?? Thanks
3
2443
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 http://news.google.com/news?hl=en&q=proxy&um=1&ie=UTF-8&imgefp=Q-gUBRtWxEsJ&imgurl=cache.boston.com/resize/bonzai-fba/Globe_Photo/2008/06/19/1213929665_4899/539w.jpg http://www.google.com/search?hl=en&q=proxy I have written a query that gets me...
0
9810
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9656
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10821
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10527
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10571
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10241
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9358
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7773
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6973
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...

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.