Question

Our accounting application is using Pervasive SQL 10. I need to fetch data of products from it. Problem is that the "name" column has fixed length of 12 and the application is filling the rest with spaces.

So every time I use my PHP script to fetch data, I need to fill the rest of the name with spaces to match it in WHERE clause.

Example data in the column:

  • 65LD11
  • 42BRD03
  • 65LD112
  • (space)65LD12
  • 165LD12

I have been using: SELECT * FROM products WHERE name LIKE '65LD12%';. Which is not perfect, but the biggest problem is with the name with space as first character, because I can't use _ or % as it would match both 65LD12 and 165LD12 name.

There can be any number of spaces at the beginning or at the end. In MySQL I would use REGEXP_LIKE to match only the spaces, but here in Pervasive I am kind of lost. Is there some way how to do this?

Was it helpful?

Solution

I don't know about Pervasive, but in Standard SQL you can do a simple

WHERE TRIM(name) = '65LD12'

Of course it would be better to clean the data and remove unnecessary leading spaces, TRIM will prevent the usage on an index. And then name = '65LD12' should return the correct data regardless of trailing blanks (again, I don't know if Pervasive implements that correctly)

edit based on comments:

There's no TRIM in Pervasive, but LTRIM:

WHERE LTRIM(name) = '65LD12'

If this is still not returning the correct rows (i.e. Pervasive implemented string comparison in a wrong way) you have to add RTRIM, too:

WHERE RTRIM(LTRIM(name)) = '65LD12'

OTHER TIPS

Try this:

SELECT * FROM products WHERE REPLACE(name,' ','') LIKE '65LD12%';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top