0

在我的 Access 数据库中,我有一个名为customers. 在此表中,我有一个名为DateEntered. 该字段的数据类型是短文本

此列中的值不一致 - 它们有几种变体:

  • MM-DD-YYYY,
  • MMDDYYYY
  • MM/DD/YYYY.

好像没有什么标准。

我的目标是选择 2012 年的所有客户。我试过了

select *
from customers
where DateEntered <('%2013') AND >('%2012');

但是当我运行它时它会出现空白。

谁能指出我做错了什么,更重要的是解释为什么这个查询在 Access 中不起作用?根据我对 SQL 的理解(不是很高级),这应该可行。

4

3 回答 3

2

另一种变体)

select * from customers where RIGHT(DateEntered, 4) = '2012'
于 2013-02-22T01:34:21.560 回答
1

If you have control over the database and application code, the best way to handle this is to use an actual Date field instead of text in the table.

One way to handle this would be to add a new field to the table, write a query or two to correctly convert the text values to actual date values, and populate the new field. At this point, you would then need to hunt down the application code the refers to this field in any way and adjust to treat the field as a date, not text. This includes your insert and update statements, report code, etc.

Finally, as a last step, I would rename the original text field (or remove it altogether) and rename the new date field to the original field name. Once you fix the problem, querying against the field will be a piece of cake.

Alternatively, if you can't alter the table and source code, you can use the date conversion function CDATE() to convert the text value to an actual date. Note that you may need to guard against non-date entries (NULL or empty string values, as well as other text values that aren't really dates in the first place). The IsDate() function can be your friend here.

If you have the time and patience, fixing the data and code is the better approach to take, but sometimes this isn't always feasible.

于 2013-02-22T01:05:17.693 回答
1

你为什么不使用LIKE运算符(当你有一个使用%and的模式时它们是合适的_):

select * from customers where DateEntered  like '%2013' or DateEntered  like '%2012'
于 2013-02-22T00:56:17.117 回答