Skip to content
Advertisement

Efficient way to ignore whitespace in DB2?

I am running queries in a large IBM DB2 database table (let’s call it T) and have found that the cells for column Identifier tend to be padded not just on the margins, but in between as well, as in: ‘ ID1 ID2 ‘. I do not have rights to update this DB, nor would I, given a number of factors. However, I want a way to ignore the whitespace AT LEAST on the left and right, even if I need to simply add a couple of spaces in between. The following queries work, but are slow, upwards of 20 seconds slow….

Trying to query something like “Select * FROM T WHERE REPLACE(Identifier, ‘ ‘, ”)…” of course just freezes up Access until I Ctrl+Break to end the operation. Is there a better, more efficient way to ignore the whitespace?

================================

UPDATE: As @Paul Vernon describes below, “Trailing spaces are ignored in Db2 for comparison purpose, so you only need to consider the leading and embedded spaces.”

This led me to generate combinations of spaces before ‘ID1’ and ‘ID2’ and select the records using the IN clause. The number of combinations means that the query is slower than if I knew the exact match. This is how it looks in my Java code with Jdbc (edited to make it more generic to the key issue):

Advertisement

Answer

Trailing spaces are ignored in Db2 for comparison purpose, so you only need to consider the leading and embedded spaces.

Assuming there is an index on the Identifier, your only option (if you can’t change the data, or add a functional index or index a generated column), is probably something like this

which the Db2 optimize might implement as 6 index lookups, which would be faster than a full index or table scan

You could also try this

which the Db2 optimize might implement as 3 index range scans,

In both examples add more lines to cover the maximum number of leading spaces you have in your data if needed. In the first example add more lines for the embeded spaces too if needed

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement