简体   繁体   English

输出参数是字符串还是日期?

[英]Is output parameter a string or date?

Please see the DDL below: 请参见下面的DDL:

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

Please see the ADODB recordset below: 请参见下面的ADODB记录集:

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. 无论数据库中的日期是什么,msgbox始终出现。

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? 在第一个代码片段中,bookingdate是否被视为字符串?

I believe the following webpage would give me the answer: http://www.w3schools.com/ado/ado_datatypes.asp . 我相信以下网页会给我答案: http : //www.w3schools.com/ado/ado_datatypes.asp However, it says Internal Server Error. 但是,它说内部服务器错误。

You should never write code like this anyway: 无论如何,您永远都不应编写这样的代码:

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

Instead get in the habit of not relying on the default property, which can backfire on you depending on the usage context. 而是养成不依赖默认属性的习惯,默认属性可能会因使用情况而适得其反。 Preferable: 首选:

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

But a huge piece you left out of the puzzle is what DBMS you are using. 但是您没有解决的一大难题就是您正在使用什么DBMS。 Some use some pretty funky data types and call them DATETIME so you probably want to look at both the ADO type of the returned Field and try displaying TypeName(rs("bookingdate").Value) to see what you're ending up with in your code. 有些使用一些非常时髦的数据类型并将其称为DATETIME因此您可能希望同时查看返回的Field的ADO类型,并尝试显示TypeName(rs("bookingdate").Value)来查看最终的结果您的代码。

If the returned data is of some type such as Decimal or String then all bets are off and you need to go through the right set of gyrations to convert it to something useful before attempting comparisons. 如果返回的数据是某种类型的数据(例如, DecimalString那么所有的选择都将关闭,您需要经过正确的旋转来将其转换为有用的数据,然后再尝试进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM