以下查询不会返回任何值。
SELECT o.ea_guid as CLASSGUID, o.Object_Type as CLASSTYPE,
o.Name, type.Property, type.Value
FROM ((t_object as o
INNER JOIN t_objectproperties as location
ON o.Object_ID = location.Object_ID)
INNER JOIN t_objectproperties as type
ON o.Object_ID = type.Object_ID)
WHERE location.Property = 'Location'
AND location.Value = 'ND'
AND type.Property = 'FDate'
AND CDate(type.Value) >= CDate('21/02/2014')
AND CDate(type.Value) <= CDate('24/02/2014')
但是,如果我执行以下操作,则会返回一组不在所需范围之间的结果
SELECT o.ea_guid as CLASSGUID, o.Object_Type as CLASSTYPE,
o.Name, type.Property, type.Value
FROM ((t_object as o
INNER JOIN t_objectproperties as location
ON o.Object_ID = location.Object_ID)
INNER JOIN t_objectproperties as type
ON o.Object_ID = type.Object_ID)
WHERE location.Property = 'Location'
AND location.Value = 'ND'
AND type.Property = 'FDate'
AND type.Value >= CDate('21/02/2014')
AND type.Value <= CDate('24/02/2014')
我还尝试使用BETWEEN
并返回范围之外的结果。
SELECT o.ea_guid as CLASSGUID, o.Object_Type as CLASSTYPE,
o.Name, type.Property, type.Value
FROM ((t_object as o
INNER JOIN t_objectproperties as location
ON o.Object_ID = location.Object_ID)
INNER JOIN t_objectproperties as type
ON o.Object_ID = type.Object_ID)
WHERE location.Property = 'Location'
AND location.Value = 'ND'
AND type.Property = 'FDate'
AND type.Value BETWEEN CDate('21/02/2014')
AND <= CDate('24/02/2014')
答案 0 :(得分:1)
日期字符串的问题是01/02/2014
之类的日期可以解释为dd/MM/yyyy
或MM/dd/yyyy
。更好地以反向格式存储日期:yyyy/MM/dd
。我从未见过像yyyy/dd/MM
这样的格式。因此,对于日期和月份的混淆应该是安全的。
反向格式的另一个优点是其字符串表示与其日期表示的方式相同。即以yyyy/MM/dd
格式排序字符串会产生正确的顺序。因此,没有必要将它们转换为比较日期。
尝试选择Month(CDate(type.Value))
并查看这是否真的总是会返回几个月,或者是否有时会返回几天。如果返回天数,您可以尝试转换像这样的属性
DateSerial(Mid$(type.Value, 7) , Mid$(type.Value, 4, 2), Mid$(type.Value, 1, 2))
答案 1 :(得分:1)
所以,我尝试了这个,它起作用了。您可以将type.Value
转换为日期,然后将Format()
转换为短日期,并按原样传递参数。无需弄乱Access无法真正关心的日期格式。只需格式化即可。
SELECT t1.ID, t1.dateString
FROM Table1 AS t1
WHERE Format(CDate(t1.dateString), "Short Date") < Format(#03/31/2014#, "Short Date");
dateString
是一个文本字段(就像你说的那样),我测试过的值是:
+-------------+
| 10/01/2014 |
+-------------+
| 20/02/2014 |
+-------------+
| 31/03/2014 |
+-------------+
以下是使用BETWEEN
的SQL:
SELECT t1.ID, t1.dateString
FROM Table1 AS t1
WHERE Format(CDate(t1.dateString), "Short Date")
Between
Format(#03/30/2014#, "Short Date")
AND
Format(#01/01/2014#, "Short Date")