DataTable.Select 在 NULL DateTime 列上使用 ISNULL 运算符时表现奇怪
我有一个带有日期时间列“DateCol”的数据表,该列可以是 DBNull。 DataTable 有一行,该列中有 NULL 值。
我正在尝试查询此列中具有 DBNull 值或大于今天日期的日期的行。今天的日期是 2010 年 5 月 11 日。我构建了一个查询来选择我想要的行,但它没有按预期工作。查询是:
string query = "ISNULL(DateCol, '" + DateTime.MaxValue + "'") > "' + DateTime.Today "'"
这导致以下查询:
"ISNULL(DateCol, '12/31/9999 11:59:59 PM') > '5/11/2010'"
当我运行此查询时,我没有得到任何结果。我花了一段时间才弄清楚原因。以下是我在 Visual Studio 即时窗口中的调查:
<前><代码>> dt.行数.Count 1 > dt.Rows[0]["DateCol"] {} > dt.Rows[0]["DateCol"] == DBNull.Value 真的 > dt.Select("ISNULL(DateCol,'12/31/9999 11:59:59 PM') > '5/11/2010'").Length 0 <-- 我预计 1
试验和错误显示日期检查在以下边界处存在差异:
<前><代码>> dt.Select("ISNULL(DateCol, '12/31/9999 11:59:59 PM') > '2/1/2000'"). Length 0 > dt.Select("ISNULL(DateCol, '12/31/9999 11:59:59 PM') > '1/31/2000'"). Length 1 <-- 这是预期的答案
如果我将 DateTime 字段用 # 而不是引号括起来,则查询工作正常。
<前><代码>> dt.Select("ISNULL(DateCol, #12/31/9999#) > #5/11/2010#"). Length 1
我的机器的区域设置当前设置为 EN-US,短日期格式为 M/d/yyyy。
为什么原始查询返回错误结果?
如果将日期与 1/31/2000 进行比较而不是与 2/1/2000 进行比较,为什么它会正常工作?
I have a DataTable with a DateTime column, "DateCol", that can be DBNull. The DataTable has one row in it with a NULL value in this column.
I am trying to query rows that have either DBNull value in this column or a date that is greater than today's date. Today's date is 5/11/2010. I built a query to select the rows I want, but it did not work as expected. The query was:
string query = "ISNULL(DateCol, '" + DateTime.MaxValue + "'") > "' + DateTime.Today "'"
This results in the following query:
"ISNULL(DateCol, '12/31/9999 11:59:59 PM') > '5/11/2010'"
When I run this query, I get no results. It took me a while to figure out why. What follows is my investigation in the Visual Studio immediate window:
> dt.Rows.Count 1 > dt.Rows[0]["DateCol"] {} > dt.Rows[0]["DateCol"] == DBNull.Value true > dt.Select("ISNULL(DateCol,'12/31/9999 11:59:59 PM') > '5/11/2010'").Length 0 <-- I expected 1
Trial and error showed a difference in the date checks at the following boundary:
> dt.Select("ISNULL(DateCol, '12/31/9999 11:59:59 PM') > '2/1/2000'").Length 0 > dt.Select("ISNULL(DateCol, '12/31/9999 11:59:59 PM') > '1/31/2000'").Length 1 <-- this was the expected answer
The query works fine if I wrap the DateTime field in # instead of quotes.
> dt.Select("ISNULL(DateCol, #12/31/9999#) > #5/11/2010#").Length 1
My machine's regional settings is currently set to EN-US, and the short date format is M/d/yyyy.
Why did the original query return the wrong results?
Why would it work fine if the date was compared against 1/31/2000 but not against 2/1/2000?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查询表达式格式使用 #...# 作为日期时间值。单引号用于字符串值。在 DateTime 周围使用单引号的表达式中,它正在进行字符串比较,其中“12/31/9999”中的字符“1”位于“5/11/2010”中的“5”之前,并且按 Unicode 顺序,“2/1/2000”中的“2”,但不是“1/31/2000”中的“1”。
The query expression format uses #...# for DateTime values. Single quotes are used for string values. In the expressions where you are using single quotes around the DateTime, it is doing a string comparison, in which the character "1" in "12/31/9999" comes before the "5" in "5/11/2010" and the "2" in "2/1/2000", but not the "1" in "1/31/2000", in Unicode order.