0

Please see the DDL below:

CREATE TABLE TestDate (bookingdate datetime)
INSERT INTO TestDate VALUES ('2013-10-04')

Please see the ADODB recordset below:

rs.open "SELECT bookingdate FROM TestDate"
If rs("bookingdate") > dateadd("yyyy", -6, Now)
  msgbox("test")
end if

The msgbox always appears regardless of what the date in the database is.

I believe I have to do this:

If datevalue(rs("bookingdate")) > dateadd("yyyy", -6, Now)
  msgbox("test")
end if

Then the messagebox only appears if the booking date is within the last six years.

Is bookingdate treated as a string in the first code fragment?

I believe the following webpage would give me the answer: http://www.w3schools.com/ado/ado_datatypes.asp. However, it says Internal Server Error.

4

1 回答 1

2

无论如何,您都不应该编写这样的代码:

If rs("bookingdate") > DateAdd("yyyy", -6, Now)

相反,养成不依赖默认属性的习惯,这可能会适得其反,具体取决于使用上下文。首选:

If rs("bookingdate").Value > DateAdd("yyyy", -6, Now)

但是您遗漏的一大块是您正在使用的 DBMS。有些使用一些非常时髦的数据类型并调用它们DATETIME,因此您可能想要查看返回字段的 ADO 类型并尝试显示TypeName(rs("bookingdate").Value)以查看您在代码中的最终结果。

如果返回的数据是某种类型,例如DecimalString然后所有的赌注都关闭了,您需要通过正确的旋转集将其转换为有用的东西,然后再尝试比较。

于 2013-10-04T22:07:43.247 回答