简体   繁体   English

带WHERE的普及SQL-匹配空间

[英]Pervasive SQL with WHERE - matching spaces

Our accounting application is using Pervasive SQL 10. I need to fetch data of products from it. 我们的会计应用程序正在使用Pervasive SQL10。我需要从中获取产品数据。 Problem is that the "name" column has fixed length of 12 and the application is filling the rest with spaces. 问题是“名称”列的固定长度为12,应用程序用空格填充其余部分。

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. 因此,每次使用PHP脚本获取数据时,都需要在WHERE子句中用空格填充该名称的其余部分。

Example data in the column: 列中的示例数据:

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

I have been using: SELECT * FROM products WHERE name LIKE '65LD12%'; 我一直在使用: 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. 这并不完美,但是最大的问题是名称以空格作为第一个字符,因为我不能使用_%因为它会同时匹配65LD12和165LD12名称。

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. 在MySQL中,我将使用REGEXP_LIKE仅匹配空格,但是在Pervasive中,我有点迷失了。 Is there some way how to do this? 有什么办法可以做到这一点?

I don't know about Pervasive, but in Standard SQL you can do a simple 我不了解Pervasive,但是在Standard SQL中,您可以做一个简单的

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. 当然,清理数据并删除不必要的前导空格会更好,TRIM将阻止在索引上使用。 And then name = '65LD12' should return the correct data regardless of trailing blanks (again, I don't know if Pervasive implements that correctly) 然后name ='65LD12'应该返回正确的数据,而不管尾随空格如何(同样,我不知道Pervasive是否正确实现了该功能)

edit based on comments: 根据评论进行编辑:

There's no TRIM in Pervasive, but LTRIM: 普适性中没有TRIM,但是LTRIM:

WHERE LTRIM(name) = '65LD12'

If this is still not returning the correct rows (ie Pervasive implemented string comparison in a wrong way) you have to add RTRIM, too: 如果仍然不能返回正确的行(即以错误的方式进行了Pervasive实现的字符串比较),则还必须添加RTRIM:

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

尝试这个:

SELECT * FROM products WHERE REPLACE(name,' ','') LIKE '65LD12%';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM