We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

ignore trailing blanks

tstrob
tstrob asked
on
Medium Priority
1,315 Views
Last Modified: 2008-02-01
I want my database to ignore trailing blanks in character variables.
Select ... where something = 'ABC'
should have the same result as
Select ... where something = 'ABC    '

I found the blank_trimming parameter which was set to false, but setting it true has no change.

Oracle 7.3.3 is running
Comment
Watch Question

Commented:
Hello !

By the Grace of God, I have the beautiful solution for your problem.

Let us assume that your datatype width of the name is 30,

Do the following query

SELECT ............................
FROM   ............................
WHERE RPAD(name,30,' ') = RPAD('ABC',30,' ');

This is tested and it works fine.
I hope this helps you.

Good Luck !

Author

Commented:
Sorry, this does not help me, as I cannot change the select-statements.
I know, that the database can do what I want, but I don´t know how...

Author

Commented:
Sorry, this does not help me, as I cannot change the select-statements.
I know, that the database can do what I want, but I don´t know how...

Commented:
tstrob,

BLANK_TRIMMING doesn't actually trim any values unless you're trying to insert a string that's too big for the column. In that case Oracle will trim blanks off  the string so that it fits in the column.

If you can't change the select statements, then you have to change your data. If you can change your insert statements, then you can just replace 'string' with 'RTRIM(string)' so that you never get those blanks into the database in the first place.

If you can't change the inserts either, then you can use a BEFORE INSERT trigger to do the same thing.

You'll also have to write a script to change your existing data. Something like this:

update mytable
set mystring = RTRIM(mystring);

Yuval


Commented:
Another thought....

Using a view might also work. Rename your table, then create a view with the name the table had. The view does the "select RTRIM(mystring)" before it gets to your application.

Author

Commented:
Sorry, but this doesn´t work for me too.
My database is ok (no blanks in the strings)

But the software that accesses the database is a little bit strange.

A special function in this software dds some blanks in the whereclause:
SELECT * where value = 'ABCD    '


Commented:
That sounds like a bug in your database access software, then. I think your best bet is to get the software fixed. Otherwise, even if you come up with some kludge, you're bound to run into problems later.

Author

Commented:
Of course the software is the problem, but this bug only appears within a particular operation.

Is there definitively no way to make  'ABC   '  match 'ABC' in the database ?



Commented:
    Tstrob ,does the function add blanks for the whole width of field.

     If this is the case, perhaps using a view might also work after all.
 
    Like yoren said "Rename your table, then create a view with
     the name the table had" .
     
     But instead of using RTRIM use RPAD to add the missing blanks.

 

     

Author

Commented:
pviaene,

I don´t know how many blanks are added before...

Commented:
Is the table only needed for SELECT statement or are there also inserts/updates/deletes

Greetings

Jan Casteels

Author

Commented:
SELECTs only !

Author

Commented:
Adjusted points to 100
Maybe the original software was written for the CHAR data type rather than the VARCHAR2 datatype, in which case it would expect all the strings to be the same length. If this was the case and you knew the original length of the field then you could use a view to RPAD (i.e add trailing spaces upto the legnth you require) the column as suggested above.

However using the view will adversely affect other code which accesses the data that does not expect 'ABC  ' instead of 'ABC'.

Vlad.

Commented:
Is there any part of the SQL-statement that can be altered?
Which part of the where statements contains the spaces, is it the database part or is it the literal part?

Greetz

Jan

Commented:
to have the 2 select the same result
"Select ... where something = 'ABC'"
"Select ... where something = 'ABC    '"

you should use a select like this
Select ... where something like 'ABC*'

But you can't change the select. just a suggestion like that.

Commented:
I believe vlad_impala is correct.
The table is probably created with column of the CHAR type in
stead of the VARCHAR2 datatype. You should ask your database
administrator to alter this.
(ALTER TABLE MODIFY (columnname VARCHAR2(length));

Oracle will consider two VARCHAR2-strings : 'ABC ' to 'ABC' as equal but not if they are of type CHAR.

Peter.

Author

Commented:
The relevant column is a varchar2(10), but for Oracle 'BLA   ' is not the same as 'BLA'

Commented:
tstrob,

Check your queries to find the pattern of blanks that are added (are they a set number of blanks, is it padding to make the string a certain length, etc), and make a view to make your query happy. Forget the char/varchar2 thing. 'BLA' is never equal to 'BLA    '.

Commented:
Here the final answer :

Change the relevant column into a CHAR(10) :

  DNAME VARCHAR2;
  LOC   CHAR(10);

SQL> select * from dept where loc = 'DALLAS';

   DEPTNO DNAME          LOC
--------- -------------- -------------
       20 RESEARCH       DALLAS

SQL> select * from dept where loc = 'DALLAS ';

   DEPTNO DNAME          LOC
--------- -------------- -------------
       20 RESEARCH       DALLAS


SQL> select * from dept where dname = 'ACCOUNTING';

   DEPTNO DNAME          LOC
--------- -------------- -------------
       10 ACCOUNTING     NEW YORK

SQL> select * from dept where dname = 'ACCOUNTING ';

no rows selected   !!!!!!!!!!

So there is no difference between looking with and without trailing spaces when you use a CHAR.

Met vriendelijke groet,
Jannes Stevens

Hi :)

Instead of renaming the table and creating view with the orignal table name. U can do one thing that u do create a view and than create a private synonym under the user which is accessing that table with the same table name on the view. So following are the steps

create or replace view myview as
select field1, rpad(field2,xyz), ...
from orignal_table
/
xyz <- is the field length of the column.

then u connect as system if u can otherwise ask your dba to create a private synonym under the user or ask dba to give create synonym privilege to the user.

create synonym orignal_table for schemaowner.myview;

Hope this help or give some ideas to continue :)

Commented:
Can you please give any comment to the rejected answer ..

Author

Commented:
jannes,
your suggestion was already made above

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
thanks, it works now !
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.