473,396 Members | 1,892 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,396 software developers and data experts.

Strange behaviour when using LIKE with %

I am running a query from a Java app via the IBM type 4 driver back to
MF DB2 and am getting a strange result.

Basically my statement is

SELECT BLAH WHERE NAME LIKE ?

....and I pass in "Fr%" expecting it to match with "Fred" but it doesnt.

....but if I pass in "Fr%%" then it does match "Fred".

Several other combinations - "Sa%%y" matching "Sally" but "Sa%y" NOT
matching "Sally" seem to suggest that the '%' is being treated as a
single char wild card and not a 1 to many wildcard?

Am I going mad or should "Fr%" match "Fred"?
Thanks

David Bevan
http://www.davidbevan.co.uk

Nov 12 '05 #1
7 3830
ju***@davidbevan.co.uk wrote:
Am I going mad or should "Fr%" match "Fred"?


While I may be tempted to answer first part of the question - it would be
sufficient to test from CLP that (using SAMPLE database):

select * from org where location like 'D%'

works fine (i.e delivers two qualifying rows. The same 2 qualifying rows are
delivered when running same query from JCC T4 driver.

So something is wrong with your code. Need more input.

Jan M. Nelken

Nov 12 '05 #2
I am trying to so a case insensetive select and so my where clause
looks like...

"where lcase(firstname) like lcase(cast(? as char(20)) )"

....but one of my colleages thinks this is dodgy SQL - plus it works if
I do...

"where lcase(firstname) like ?"

and do the toLowerCase() in Java.

So is the first where clause bad SQL?

David Bevan
http://www.davidbevan.co.uk

Nov 12 '05 #3
If the SQL statement is using a variable, such as in a routine, and if
that variable is declared as CHAR, it will have this problem, because
all non-defined characters become spaces and must be matched.
and I pass in "Fr%" expecting it to match with "Fred" but it doesnt.
Because it is actually searching for "Fr% "
"Sa%%y" matching "Sally" but "Sa%y" NOT matching "Sally" seem to
suggest that the '%' is being treated as a single char wild card


Or that it is searching for one more trailing space.

To resolve, either declare the IN variable as VARCHAR, or replace all
spaces with wildcards REPLACE(Search, ' ', '%')

B.

Nov 12 '05 #4
The "bad SQL" is anytime a WHERE clause does a single-row FUNCTION on a
COLUMN, because that negates the use of any INDEXes and forces a full
table scan.

I have a query where i needed to search some text excluding records
that had a caseless "x-" prefix in one of their fields. So, i used:

AND Text NOT LIKE 'X-%'
AND Text NOT LIKE 'x-%'

If you can figure out some other rules so the COLUMN can be used
without a FUNCTION, it would be "better SQL". Either that, or ADD a
COLUMN to the TABLE as a GENERATED ALWAYS AS LCASE(firstname). This
way, the original COLUMN is kept as is, and another COLUMN already has
it lowercased and would be availible for INDEXing.

As for where to lowercase the text, i doubt it matters, unless other
criteria is used for searching the first name. (This is something i
know from Oracle, i am not sure how DB2 does it.) Because, this will
affect how the query is stored. If the same query comes up a second
time, it would not need to be re-optimized if the text is exactly the
same. However, if you lcase in one query but not in the next, even
though the rest of the query is identical, the query will be
re-optimized. The time wasted is usually negligable, but can help to
bear in mind if these query are executed many times. So, it is good
practice to lowercase it in your java code instead.

B.

Nov 12 '05 #5
Ian
ju***@davidbevan.co.uk wrote:
I am running a query from a Java app via the IBM type 4 driver back to
MF DB2 and am getting a strange result.

Basically my statement is

SELECT BLAH WHERE NAME LIKE ?

...and I pass in "Fr%" expecting it to match with "Fred" but it doesnt.

...but if I pass in "Fr%%" then it does match "Fred".

Several other combinations - "Sa%%y" matching "Sally" but "Sa%y" NOT
matching "Sally" seem to suggest that the '%' is being treated as a
single char wild card and not a 1 to many wildcard?

Am I going mad or should "Fr%" match "Fred"?


Not sure about this, but I'd guess that using parameter markers and
SQL wildcards has different symantecs than straight SQL.

That is, a single percent '%' is probably being interpreted as a
literal percent sign, but '%%' is being interpreted as the wildcard
(similar to how you embed a escape single quote in a value, as in
select id from table where word = 'don''t')

So 'Fr%' would match 'Fr%', not 'Fra', 'Fre', 'Frx' etc.

Nov 12 '05 #6
That shouldn't be the case, because it is a LIKE, and therefore unless
he used the ESCAPE parameter, the two globbing operators should not be
treated as literals. DB2 follows that standard. <URL:
http://publib.boulder.ibm.com/infocenter/db2help/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0000751.htm>

B.

Nov 12 '05 #7
The first SQL clause with the cast demands that your input be padded with
spaces to exactly 20 characters. For your sample input 'Fr%' you actually
have 17 exact-match blanks ('Fr% ').' Your query is doing
exactly what you asked it to, but apparently that's not what you want.

<ju***@davidbevan.co.uk> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I am trying to so a case insensetive select and so my where clause
looks like...

"where lcase(firstname) like lcase(cast(? as char(20)) )"

...but one of my colleages thinks this is dodgy SQL - plus it works if
I do...

"where lcase(firstname) like ?"

and do the toLowerCase() in Java.

So is the first where clause bad SQL?

David Bevan
http://www.davidbevan.co.uk

Nov 12 '05 #8

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

Similar topics

0
by: Frank Passek | last post by:
Dear all, I've encountered some strange behaviour with PHP (4.3.2) using the CLI-API. When I provide an option in the first line of my script like so: #!/usr/bin/php -c /path_to_my_ini_file and...
4
by: Ben | last post by:
Hi all, I'm trying to figure out how how complex map, filter and reduce work based on the following piece of code from http://www-106.ibm.com/developerworks/linux/library/l-prog.html : ...
3
by: Andrew Mayo | last post by:
(note: reason for posting here; browser helper object is written in C++; C++ developers tend to know the intricacies of message handling; this looks like a Windows messaging issue) Microsoft...
2
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from...
3
by: Sebastian C. | last post by:
Hello everybody Since I upgraded my Office XP Professional to SP3 I got strange behaviour. Pieces of code which works for 3 years now are suddenly stop to work properly. I have Office XP...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
4
by: ignw82 | last post by:
Hi all, I have a strange behaviour in dataview, maybe you can help me. the behaviour is like this : First I made a datatable (odt) in data set, and then I created a dataview using this...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
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?
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...
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
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,...

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.