473,883 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wildcard doesn't match using LIKE '%' on a varchar field, wierd!

Right this has to be a Micro$oft mess-up surely...?

I'm running SQL 2k standard with SP3. I have a table which I'm trying
to query using a LIKE operator on a varchar field as follows

....
WHERE dbo.tbl_pm_proj ects.SeniorMana gerID LIKE '%'
....

In actual fact the % is passed in by the application when the user
selects "All managers" from the drop down list used to select the
Manager to filter by. If they select a manager's name from the list
it becomes LIKE 'ajames' or whatever.

BUT - the table currently contains 2972 records. If I take out the
WHERE clause the SELECT returns all records - fine - but if I put the
where clause in it returns only 1682!! I thought the % was meant to
match, and I quote the SQL server Books Online files here; "Any string
of zero or more characters." Anyone explain to me whats going on
here?

TIA
Niall
Jul 20 '05 #1
12 10284
aaj
you have probably already looked at this... bit I'm just wondering if
there's a pattern to the things its not returning?

if you subrtract your results from the 'like' records from the 'full' set of
records, is there anything in the SeniorManagerID field that suffests a
pattern?


"Niall Porter" <ni*********@ya hoo.co.uk> wrote in message
news:2d******** *************** ***@posting.goo gle.com...
Right this has to be a Micro$oft mess-up surely...?

I'm running SQL 2k standard with SP3. I have a table which I'm trying
to query using a LIKE operator on a varchar field as follows

...
WHERE dbo.tbl_pm_proj ects.SeniorMana gerID LIKE '%'
...

In actual fact the % is passed in by the application when the user
selects "All managers" from the drop down list used to select the
Manager to filter by. If they select a manager's name from the list
it becomes LIKE 'ajames' or whatever.

BUT - the table currently contains 2972 records. If I take out the
WHERE clause the SELECT returns all records - fine - but if I put the
where clause in it returns only 1682!! I thought the % was meant to
match, and I quote the SQL server Books Online files here; "Any string
of zero or more characters." Anyone explain to me whats going on
here?

TIA
Niall

Jul 20 '05 #2
"Niall Porter" <ni*********@ya hoo.co.uk> wrote in message
news:2d******** *************** ***@posting.goo gle.com...
Right this has to be a Micro$oft mess-up surely...?

I'm running SQL 2k standard with SP3. I have a table which I'm trying
to query using a LIKE operator on a varchar field as follows

...
WHERE dbo.tbl_pm_proj ects.SeniorMana gerID LIKE '%'
...

In actual fact the % is passed in by the application when the user
selects "All managers" from the drop down list used to select the
Manager to filter by. If they select a manager's name from the list
it becomes LIKE 'ajames' or whatever.

BUT - the table currently contains 2972 records. If I take out the
WHERE clause the SELECT returns all records - fine - but if I put the
where clause in it returns only 1682!! I thought the % was meant to
match, and I quote the SQL server Books Online files here; "Any string
of zero or more characters." Anyone explain to me whats going on
here?

TIA
Niall


Is the column null-able?
Try
....
WHERE dbo.tbl_pm_proj ects.SeniorMana gerID is NULL
....
and see if it returns anything.
Jul 20 '05 #3
Niall Porter (ni*********@ya hoo.co.uk) writes:
Right this has to be a Micro$oft mess-up surely...?
I would not place a bet on that.
I'm running SQL 2k standard with SP3. I have a table which I'm trying
to query using a LIKE operator on a varchar field as follows

...
WHERE dbo.tbl_pm_proj ects.SeniorMana gerID LIKE '%'
...

In actual fact the % is passed in by the application when the user
selects "All managers" from the drop down list used to select the
Manager to filter by. If they select a manager's name from the list
it becomes LIKE 'ajames' or whatever.

BUT - the table currently contains 2972 records. If I take out the
WHERE clause the SELECT returns all records - fine - but if I put the
where clause in it returns only 1682!! I thought the % was meant to
match, and I quote the SQL server Books Online files here; "Any string
of zero or more characters." Anyone explain to me whats going on
here?


Any string, yes, but if there are 2972 - 1682 rows in that table where
SeniorManagerID is NULL, it all makes sense.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
ni*********@yah oo.co.uk (Niall Porter) wrote in
news:2d******** *************** ***@posting.goo gle.com:
Right this has to be a Micro$oft mess-up surely...?

I'm running SQL 2k standard with SP3. I have a table which I'm trying
to query using a LIKE operator on a varchar field as follows

...
WHERE dbo.tbl_pm_proj ects.SeniorMana gerID LIKE '%'
...

In actual fact the % is passed in by the application when the user
selects "All managers" from the drop down list used to select the
Manager to filter by. If they select a manager's name from the list
it becomes LIKE 'ajames' or whatever.

BUT - the table currently contains 2972 records. If I take out the
WHERE clause the SELECT returns all records - fine - but if I put the
where clause in it returns only 1682!! I thought the % was meant to
match, and I quote the SQL server Books Online files here; "Any string
of zero or more characters." Anyone explain to me whats going on
here?

TIA
Niall


Beating a dead horse here -- it's already been answered by others. But, a
NULL value is *not* the same as a string of zero length. NULL represents
the absence of any value, and the SQL standard says that a NULL value is
never equal to any other value including another NULL.
Jul 20 '05 #5
Thanks all for your replies. I checked the number of records returned
by the full query and the one with the LIKE '%' condition and indeed,
if I do it with WHERE dbo.tbl_pm_proj ects.SeniorMana gerID IS NULL then
I get 1290 records. 1290 + 1682 = 2972, the total number of records
in the table.

I must admit I didn't check this to start with because I'm sure this
query was returning all entries in the table previously. Is there
another wildcard or operator I can use or some config setting in the
database or the SQL server in general I can use to have it include
NULL results in queries like this?

Niall

ni*********@yah oo.co.uk (Niall Porter) wrote in message news:<2d******* *************** ****@posting.go ogle.com>...
Right this has to be a Micro$oft mess-up surely...?

I'm running SQL 2k standard with SP3. I have a table which I'm trying
to query using a LIKE operator on a varchar field as follows

...
WHERE dbo.tbl_pm_proj ects.SeniorMana gerID LIKE '%'
...

In actual fact the % is passed in by the application when the user
selects "All managers" from the drop down list used to select the
Manager to filter by. If they select a manager's name from the list
it becomes LIKE 'ajames' or whatever.

BUT - the table currently contains 2972 records. If I take out the
WHERE clause the SELECT returns all records - fine - but if I put the
where clause in it returns only 1682!! I thought the % was meant to
match, and I quote the SQL server Books Online files here; "Any string
of zero or more characters." Anyone explain to me whats going on
here?

TIA
Niall

Jul 20 '05 #6
On 6 Sep 2004 03:07:49 -0700, Niall Porter wrote:
Thanks all for your replies. I checked the number of records returned
by the full query and the one with the LIKE '%' condition and indeed,
if I do it with WHERE dbo.tbl_pm_proj ects.SeniorMana gerID IS NULL then
I get 1290 records. 1290 + 1682 = 2972, the total number of records
in the table.

I must admit I didn't check this to start with because I'm sure this
query was returning all entries in the table previously. Is there
another wildcard or operator I can use or some config setting in the
database or the SQL server in general I can use to have it include
NULL results in queries like this?

Niall


Hi Niall,

WHERE SeniorManagerID LIKE @SearchParam
OR (SeniorManagerI D IS NULL AND @SearchParam = '%')

or

WHERE COALESCE (SeniorManagerI D, '') LIKE @SearchParam
You might also want to read this: http://www.sommarskog.se/dyn-search.html

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #7
Hugo Kornelis <hugo@pe_NO_rFa ct.in_SPAM_fo> wrote in message news:<5u******* *************** **********@4ax. com>...

[snip]

WHERE SeniorManagerID LIKE @SearchParam
OR (SeniorManagerI D IS NULL AND @SearchParam = '%')

or

WHERE COALESCE (SeniorManagerI D, '') LIKE @SearchParam
Thanks Hugo, I've gone for the second option as I'm now going to have
to combine several similar queries into one, I've picked the COALESCE
option for shorter SQL code. Seems to be working nicely now, many
thanks!
You might also want to read this: http://www.sommarskog.se/dyn-search.html
Article was quite useful, again thanks. Erland, if you read this,
were you simply being modest by not pointing me at this article? Good
article all the same, I've bookmarked your site for future reference.
Best, Hugo


Thanks again all who provided responses, much appreciated.

Kind regards,
Niall
Jul 20 '05 #8
Hugo Kornelis <hugo@pe_NO_rFa ct.in_SPAM_fo> wrote in message news:<5u******* *************** **********@4ax. com>...
On 6 Sep 2004 03:07:49 -0700, Niall Porter wrote:

Hi Niall,

WHERE SeniorManagerID LIKE @SearchParam
OR (SeniorManagerI D IS NULL AND @SearchParam = '%')

or

WHERE COALESCE (SeniorManagerI D, '') LIKE @SearchParam
You might also want to read this: http://www.sommarskog.se/dyn-search.html

Best, Hugo


Hi again,

For those who are interested, I discovered another way of coping with
the NULL not being the same as a zero-length entry thing, it is to do
this:

....
WHERE ISNULL(fieldYou WantToQuery, ' ') LIKE @SearchParam

making sure there IS a space between the single quotes, ISNULL used in
this manner will substitute the space character where there are NULL
values. Maybe useful if a situation arose where using COALESCE was
not appropriate for some reason.

Now the philosophical bit - if a zero-length string is not NULL, then
what IS NULL? :)

Regards,
Niall
Jul 20 '05 #9
On 7 Sep 2004 04:11:08 -0700, Niall Porter wrote:
Hugo Kornelis <hugo@pe_NO_rFa ct.in_SPAM_fo> wrote in message news:<5u******* *************** **********@4ax. com>...
On 6 Sep 2004 03:07:49 -0700, Niall Porter wrote:

Hi Niall,

WHERE SeniorManagerID LIKE @SearchParam
OR (SeniorManagerI D IS NULL AND @SearchParam = '%')

or

WHERE COALESCE (SeniorManagerI D, '') LIKE @SearchParam
You might also want to read this: http://www.sommarskog.se/dyn-search.html

Best, Hugo


Hi again,

For those who are interested, I discovered another way of coping with
the NULL not being the same as a zero-length entry thing, it is to do
this:

...
WHERE ISNULL(fieldYou WantToQuery, ' ') LIKE @SearchParam

making sure there IS a space between the single quotes, ISNULL used in
this manner will substitute the space character where there are NULL
values. Maybe useful if a situation arose where using COALESCE was
not appropriate for some reason.

Now the philosophical bit - if a zero-length string is not NULL, then
what IS NULL? :)

Regards,
Niall


Hi Niall,

ISNULL is in fact the same as COALESCE with two arguments. The only
differences are that COALESCE is extendible to three or more arguments and
it is defined in the ISO/ANSI standards for SQL; ISNULL is proprietary
Transact-SQL syntax (dating back to the time before COALESCE was
incorporated in the SQL standards).

I don't think the space between the single quotes is needed with either
ISNULL or COALESCE. In fact, a quick test shows it isn't.

For the philosophical bit: NULL is the absence of data, usually because
you don't know the data. The middle initial of William Shakespeare is the
empty (or zero-length) string, as we all know that William Shakespeare had
no middle initials. But if I ask you for the middle initial of my brother,
you'd have to answer me with NULL, as you have no way of knowing if my
brother has any middle initial and if so, what it might be.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #10

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

Similar topics

5
10780
by: Aaron C | last post by:
Hi, I'm trying to do an insert with the following statement: INSERT INTO user VALUES ( 'ag@ag.com','ag','Aaron','Chandler','','','La Mirada','CA',90638,714,'',''); and I'm getting the error message "Column count doesn't match value count at row 1".
5
7438
by: Robert Brown | last post by:
I have researched newsgroups and the web very thoroughly and unsuccessfully for a solution to what I believe is a very common problem. I know it's easy to do wildcard match against data in DB (using LIKE and "%" and "?"). But is it possible to match a concrete string against a database of wildcarded data? ("%" and LIKE do not work). For example: CREATE TABLE blacklist (
1
7286
by: deko | last post by:
I have a form where users can enter a string with asterisks to perform a wildcard search. Currently, the string entered by the user looks like this: *somestring* The purpose is to match any database field containing "somestring". Is there a way to avoid the need for the asterisks? This would make it easier for the users and also I wouldn't have to explain that asterisks are required when performing a wildcard search.
3
2285
by: george.lengel | last post by:
Hello experts, I have been struggling for days to solve this problem and every suggestion I find via Google does not work for me. There is probably a solution out there that will do what I want, but I probably have not properly implemented the solutions I find. I am trying to make a page to allow personnel the ability to search our backend DB (which is Ingres 2.0) through a web interface. Ingres has a cgi program called ICE that can...
78
4661
by: wkehowski | last post by:
The python code below generates a cartesian product subject to any logical combination of wildcard exclusions. For example, suppose I want to generate a cartesian product S^n, n>=3, of that excludes '*a*b*' and '*c*d*a*'. See below for details. CHALLENGE: generate an equivalent in ruby, lisp, haskell, ocaml, or in a CAS like maple or mathematica. #------------------------------------------------------------------------------- # Short...
7
11004
by: eddie.holder | last post by:
Hi ladies and gents. I'm hoping anyone will be able to help me in a small access problem I am having. Let me try to explain: I have a form with textboxes which I use as criteria for a query. The form is used to allow my users to search for specific company names or if left blank, all the company names, so I built my criteria as follows: Like .. & chr(42) The form works ok if I am looking for a spesific company name, but when left...
0
4104
by: tania | last post by:
i have this table in my database: CREATE TABLE FILM( F_ID INT(5) NOT NULL AUTO_INCREMENT, F_TITLE VARCHAR(40) NOT NULL, DIRECTOR_FNAME VARCHAR(20) NOT NULL, DIRECTOR_LNAME VARCHAR(20) NOT NULL, TYPE VARCHAR(30) NOT NULL, DURATION TIME , YEAR_RELEASE YEAR NOT NULL, DESCRIPTION TEXT,
11
6535
by: ABC | last post by:
How to rename files with support wildcard ?
4
5790
by: kcddoorman | last post by:
I'm trying to make a multi criteria server filter and everything works fine when filtering for strings. When I add a number field to the filterable set I run into problems. Here is the VBScript I'm using. <SCRIPT language=vbscript> Function SetServerFilter() dim myFilter, strCustomer, strJobName, strSalesPerson, strWorkOrder If txtCustomer.value = "" Then strCustomer = " like '%'" Else strCustomer = " = '" & txtCustomer.value & "'"
0
9933
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
9781
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
10734
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...
0
10407
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
9564
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
7960
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
5793
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4210
muto222
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.