在 MS-Access 中插入带有日期时间的 SQL 命令 [英] Insert SQL command with Datetime in MS-Access
问题描述
我正在 MS-Access 2007 中尝试以下查询,但在时间字段上失败.
I am trying the following query in MS-Access 2007, but it fails on the time field.
INSERT INTO LOG (
EMPLOYEECODE, STATUSID, LOCATIONID, TIME, DURATION,
SHIFTID, LATECOMING, EARLYGOING, LOGDATE, STATIONID
)
VALUES (
1, 1, 0, '4/21/2009 2:25:53 PM', 0,
8, 0, 1, '1/1/2009', 1
)
TIME
字段定义为日期时间.
The TIME
field is defined as a datetime.
没有 TIME
字段,查询工作正常!
Without the TIME
field, the query works fine!
我尝试了许多不同的方法,例如将日期时间括在散列、引号等中.但是,时间字段上的查询仍然失败.
I've tried a number of different things, such as enclosing the datetime in hashes, quotes etc. However, the query still fails on the time field.
谢谢各位!这几乎让我完全到了那里.我仍然不断收到插入语句的语法错误,但是在进一步的谷歌搜索中,我意识到 TIME
可能是一个保留关键字,因此将它放在方括号中作为 [TIME]代码>成功了!
Thank you guys! That almost got me fully there. I still kept getting the syntax error for the insert statement, but then on further googling, I realized that TIME
might be a reserved keyword, so putting it on box brackets as [TIME]
worked!
推荐答案
日期 &access中的时间输入使用#,因为access不能在SQL查询中自动将char/text转换成日期或时间(或access调用查询),最好使用国际标准输入日期YYYY-MM-DD HH:NN:SS 的时间(4 位年份、2 位月份、2 位日期、2 位小时、2 位分钟、2 位秒)
Date & Time input in access use #, since access can't do auto conversion from char/text into date or time in SQL Query (or access call it query), and you better use international standard for inputting date time which was YYYY-MM-DD HH:NN:SS (4-digit year, 2-digit month, 2-digit day, 2-digit hour, 2-digit minute, 2-digit second)
所以对于 4/21/2009 2:25:53 PM 使用 #2009-04-21 14:25:53#
或者如果仍然失败,您可以使用#'2009-04-21 14:25:53'#
or if it still fail, you can use #'2009-04-21 14:25:53'#
如果您启用 ANSI 92 或使用 ADO/OLEDB 作为数据库接口,以上可能会起作用,感谢大卫指出
Above might be working if you enable ANSI 92 or using ADO/OLEDB as database interface, thanks David for pointing out
我建议您使用 YYYY-MM-DD HH:NN:SS 格式并在使用 # 之前尝试使用单引号 (') 就像我上面说的
I suggest you use YYYY-MM-DD HH:NN:SS format and try it with single quotes (') before use # like i said above
这篇关于在 MS-Access 中插入带有日期时间的 SQL 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!