访问 SQL 日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8526494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access SQL Date Format
提问by Asdwq Qwksf
How can I retrieve a record based on a Date property? I'm trying:
如何根据日期属性检索记录?我想:
WHERE Meetings.[MDate] = '16/12/2011'
which is the format I use but I get :
这是我使用的格式,但我得到:
"Data type mismatch in criteria expression"
“条件表达式中的数据类型不匹配”
Problem solved: It should have been:
问题已解决:应该是:
WHERE Meetings.[MDate] = 16/12/2011
No quotation marks.
没有引号。
回答by Ricardo Souza
For where clauses use
对于 where 子句使用
columnName = #mm/dd/yyyy#
回答by competent_tech
You'll want to use the SQL date format: '#2011-12-16#'
您需要使用 SQL 日期格式:'#2011-12-16#'
回答by onedaywhen
Use the cast to DATETIME
function, CDATE()
, which will honour the machine's regional settings. That said, it still a good idea to use an unambiguous date format and the ISO 8601format is a good one.
使用强制转换DATETIME
功能,CDATE()
,这将尊重机器的区域设置。也就是说,使用明确的日期格式仍然是一个好主意,而ISO 8601格式是一个很好的格式。
Also note that Access doesn't have a date data type: its sole temporal data type is DATETIME
and, as its name suggests, alwayshas a time element accurate to one second time granule, even if that time happens to be midnight. Therefore, it is a good idea to always include a time value to one second time granule in all DATETIME
literals e.g.
另请注意,Access 没有日期数据类型:它唯一的时间数据类型是DATETIME
,正如其名称所暗示的那样,始终具有精确到一秒时间粒度的时间元素,即使该时间恰好是午夜。因此,在所有DATETIME
文字中始终包含一个时间值到一秒时间颗粒是一个好主意,例如
WHERE Meetings.MDate = CDATE('2011-12-16 00:00:00');
Another advantage to the above is that the Access UI will not attempt to reformat the DATETIME
literal because it is held as a string.
上述的另一个优点是 Access UI 不会尝试重新格式化DATETIME
文字,因为它是作为字符串保存的。