简体   繁体   English

MS Access SQL'条件表达式中的数据类型不匹配',因为在子查询中滤除了空值

[英]MS Access SQL 'Data type mismatch in criteria expression' due to null value filtered out in subquery

I have a table in an Access database with a short text column that has the format '0000', '0000-00' or null. 我在Access数据库中有一个带有短文本列的表,该文本列的格式为'0000','0000-00'或null。 I need to query based on the first four numbers, and have tried the following: 我需要根据前四个数字进行查询,并尝试了以下操作:

SELECT *
FROM 
(
SELECT *
FROM Table1
WHERE Field1 IS NOT NULL
)
WHERE Val(LEFT(Field1,4)) > 1002

This query returns a 'Data type mismatch in criteria expression'. 该查询返回“条件表达式中的数据类型不匹配”。 I am almost certain that this arises from the null values, as in a test case I can get the query to work by removing the null values (ie deleting the records) from a copy of the table. 我几乎可以肯定这是由空值引起的,因为在一个测试案例中,我可以通过从表的副本中删除空值(即删除记录)来使查询工作。

I have also verified that the inner query returns only records where Field1 is not null. 我还验证了内部查询仅返回Field1不为空的记录。

It seems to me perhaps that the query engine is executing out of the order specified in the query? 在我看来,查询引擎执行的顺序可能不符合查询中指定的顺序? Or is there something I am doing wrong? 还是我做错了什么?

Use LIKE : 使用LIKE

SELECT *
FROM Table1
WHERE Field1 LIKE "1002*"

NULL values fail almost all comparisons, so there is no need for a separate NULL comparison. NULL值几乎无法通过所有比较,因此不需要单独的NULL比较。 The subquery is unnecessary. 子查询是不必要的。

For inequality, you should just be able to use string comparisons: 对于不平等,您应该只能够使用字符串比较:

WHERE field1 > "1002"

This will match "1002-00". 这将匹配“ 1002-00”。 If you don't want that, use LEFT() : 如果您不想这样做,请使用LEFT()

WHERE LEFT(field1, 4) > "1002"

Also works: 也可以:

SELECT *
FROM Table1
WHERE IIF(Field1 IS NULL, 0, Val(LEFT(Field1,4))) > 1002

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

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