If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Trailing white space is being ignored by DB2 when doing WHERE comparisons

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-27-10, 03:38
rick_swiss rick_swiss is offline
Registered User
 
Join Date: Apr 2010
Posts: 5
Trailing white space is being ignored by DB2 when doing WHERE comparisons

Below is a clear example of the problem (see second SELECT on oracle versus DB2). There must be a db2 database parameter somewhere to control this but I can't seem to find it out on the web. I need DB2 to react like Oracle below, but now DB2 appears to automatically trim trailing white space when doing WHERE comparisons. (ie. I know about RTRIM and LTRIM functions but that really isn't a solution for us). Does anyone know of any DB2 database parameter or way to make this act like oracle?

Thanks!

Consider the following behaviour with oracle:

SQL> create table bla(RECID VARCHAR(255));

Table created.

SQL> insert into bla values('abc ');

1 row created.

SQL> select recid from bla where recid = 'abc';

no rows selected

SQL> select recid from bla where recid = 'abc ';

RECID
--------------------------------------------------------------------------------
abc

SQL> select LENGTH(recid) from bla where recid = 'abc ';

LENGTH(RECID)
-------------
4

Now with DB2:

db2 => create table bla(RECID VARCHAR(255))
DB20000I The SQL command completed successfully.
db2 => insert into bla values('abc ')
DB20000I The SQL command completed successfully.
db2 => select recid from bla where recid = 'abc'

RECID
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
abc

1 record(s) selected.

db2 => select recid from bla where recid = 'abc '

RECID
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
abc

1 record(s) selected.

db2 => select LENGTH(recid) from bla where recid = 'abc '

1
-----------
4

1 record(s) selected.

db2 =>
Reply With Quote
  #2 (permalink)  
Old 04-27-10, 04:58
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
As you noticed, DB2 does not work the same as Oracle in this regard. DB2 treats 'XX' the same as 'XX ' in a compare. DB2 makes life for everyone much easier because of this, unless you are doing a conversion from Oracle to DB2, or vise versa.

I am not sure about the Oracle compatibility mode when using DB2 9.7.1 as to whether it works the way you want it to. If you can't find this in the DB2 doc, I would contact IBM (PMR).

One other thing. In DB2, = '' is not a valid test for NULL. This is the same as = ' ' in DB2. But not sure about Oracle compatibility mode.

If you are using DB2 Express-C, I don't recall if all the Oracle compatibility mode features are available to you.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 04-27-10, 05:33
rick_swiss rick_swiss is offline
Registered User
 
Join Date: Apr 2010
Posts: 5
Thanks for the reply...The oracle compatibility option seems promising...According to the docs this should work for DB2 v 9.7.x but seems not to work (my version is shown below). Any idea on what version I have to be on?

db2@chdsk-tafjdev1:~$ db2start
SQL1063N DB2START processing was successful.
db2@chdsk-tafjdev1:~$ db2set DB2_COMPATIBILITY_VECTOR=ORA

DBI1301E Invalid value.

Explanation:

The value specified for the registry variable is invalid.

User response:

Refer to the DB2 Information Center to determine the valid values for
the registry variable.


db2@chdsk-tafjdev1:~$ db2level
DB21085I Instance "db2" uses "32" bits and DB2 code release "SQL09070" with
level identifier "08010107".
Informational tokens are "DB2 v9.7.0.0", "s090521", "LINUXIA3297", and Fix Pack
"0".
Product is installed at "/home/db2/sqllib".
Reply With Quote
  #4 (permalink)  
Old 04-27-10, 08:30
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Oracle compatibility is limited in DB2 Express-C. You need at least Workgroup Server Edition to get the full range of compatibility features.
Reply With Quote
  #5 (permalink)  
Old 04-27-10, 08:31
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Lightbulb

Quote:
Originally Posted by rick_swiss View Post
Below is a clear example of the problem (see second SELECT on oracle versus DB2). There must be a db2 database parameter somewhere to control this but I can't seem to find it out on the web. I need DB2 to react like Oracle below, but now DB2 appears to automatically trim trailing white space when doing WHERE comparisons. (ie. I know about RTRIM and LTRIM functions but that really isn't a solution for us). Does anyone know of any DB2 database parameter or way to make this act like oracle?

Thanks!

Consider the following behaviour with oracle:

SQL> create table bla(RECID VARCHAR(255));

Table created.

SQL> insert into bla values('abc ');

1 row created.

SQL> select recid from bla where recid = 'abc';

no rows selected

SQL> select recid from bla where recid = 'abc ';

RECID
--------------------------------------------------------------------------------
abc

SQL> select LENGTH(recid) from bla where recid = 'abc ';

LENGTH(RECID)
-------------
4

Now with DB2:

db2 => create table bla(RECID VARCHAR(255))
DB20000I The SQL command completed successfully.
db2 => insert into bla values('abc ')
DB20000I The SQL command completed successfully.
db2 => select recid from bla where recid = 'abc'

RECID
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
abc

1 record(s) selected.

db2 => select recid from bla where recid = 'abc '

RECID
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
abc

1 record(s) selected.

db2 => select LENGTH(recid) from bla where recid = 'abc '

1
-----------
4

1 record(s) selected.

db2 =>
Spaces did not trim from trail of the righ side of equaty, but DB2 added spaces on the left side of conditition (if length of the left is not equal length of the right), how I know.

Kara
Reply With Quote
  #6 (permalink)  
Old 04-27-10, 10:46
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Wink DB2 made easy, again

You can solve your problem very easy, as usual:

Instead of:
Code:
select recid from bla where recid = 'abc '
You have to use:
Code:
select recid from bla where recid || '*' = 'abc ' || '*'
Lenny
Reply With Quote
  #7 (permalink)  
Old 04-27-10, 12:30
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Exclamation Performance

Thinking about performance, you can use following query:

Code:
select recid from bla 
where recid = 'abc' and length(recid) = length('abc');
Lenny
Reply With Quote
  #8 (permalink)  
Old 04-27-10, 15:22
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by rick_swiss View Post
Thanks for the reply...The oracle compatibility option seems promising...According to the docs this should work for DB2 v 9.7.x but seems not to work (my version is shown below). Any idea on what version I have to be on?

db2@chdsk-tafjdev1:~$ db2level
DB21085I Instance "db2" uses "32" bits and DB2 code release "SQL09070" with
level identifier "08010107".
Informational tokens are "DB2 v9.7.0.0", "s090521", "LINUXIA3297", and Fix Pack
"0".
Product is installed at "/home/db2/sqllib".
You should definitely upgrade to fixpack 1, since Oracle compatibility was enhanced in that fixpack. However, if you are using DB2 Express-C, not sure if that is available.

Fixpack 2 will be out shortly, and would adivse anyone using 9.7 to apply it.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #9 (permalink)  
Old 04-29-10, 08:14
rick_swiss rick_swiss is offline
Registered User
 
Join Date: Apr 2010
Posts: 5
Lenny, it isn't that simple for a variety of reasons that have to do with our internal software. Nonetheless we might have to resort to that if the Oracle Compatibility features don't do the job.
Reply With Quote
  #10 (permalink)  
Old 04-29-10, 09:31
rick_swiss rick_swiss is offline
Registered User
 
Join Date: Apr 2010
Posts: 5
Just confirmed that using Oracle Compatibility mode does solve this issue with version 9.7.0 Workgroup Server Edition...The steps are:

db2set DB2_COMPATIBILITY_VECTOR=ORA
db2stop
db2start

create your database...Seems that after creating the database it stays in this mode even after you do a db2set DB2_COMPATIBILITY_VECTOR= . So that's nice...More info here:

IBM DB2 9.7 for Linux, UNIX and Windows Information Center
Reply With Quote
  #11 (permalink)  
Old 04-29-10, 09:47
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
If you are still on 9.7.0 (fixpack 0), you definitely need to upgrade to fixpack 1 which has improved Oracle compatibility, plus some major bug fixes.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #12 (permalink)  
Old 04-29-10, 10:01
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb DB2 has the same problem

Quote:
Originally Posted by rick_swiss View Post
Lenny, it isn't that simple for a variety of reasons that have to do with our internal software. Nonetheless we might have to resort to that if the Oracle Compatibility features don't do the job.
I don't know about Oracle's problem, but the same problem exists in DB2 and I shown to you ways to solve this problem.

Lenny
Reply With Quote
  #13 (permalink)  
Old 04-29-10, 10:39
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by Lenny77 View Post
I don't know about Oracle's problem, but the same problem exists in DB2 and I shown to you ways to solve this problem
I don't think that either Oracle or DB2 have a "problem," they just work differently. I definitely prefer the way DB2 handles trailing spaces, which I believe is the ANSI standard (Oracle gave up on ANSI standards long ago).
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #14 (permalink)  
Old 04-29-10, 11:02
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Quote:
Originally Posted by Marcus_A View Post
I don't think that either Oracle or DB2 have a "problem," they just work differently. I definitely prefer the way DB2 handles trailing spaces, which I believe is the ANSI standard (Oracle gave up on ANSI standards long ago).
No, sir ! There is the same problem in DB2 exist. I think, this is common problem for all data bases.

Lenny
Reply With Quote
  #15 (permalink)  
Old 04-29-10, 12:31
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by Lenny77 View Post
No, sir ! There is the same problem in DB2 exist. I think, this is common problem for all data bases.

Lenny
What problem are you talking about?

'ABC ' = 'ABC' sounds good to me!!! And it sounds good to every programmer I ever met.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
Reply

Tags
db2, oracle, space, string literals

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On