为数据表中的计算列定义 DateDiff

发布于 2024-08-28 19:02:49 字数 434 浏览 3 评论 0原文

我有 DateTimeExpired 列,我想创建另一个名为“Expired”的列,该列将根据到期日期显示“是”或“否” - 如果日期已过,则显示“是”。
我写了这个:

DataColumn colExpirationDate = new DataColumn("DateTimeExpired", typeof(DateTime));
DataColumn colExpired = new DataColumn("Expired", typeof(string), "IIF(DateDiff(DateTimeExpired, date())>= 0,'No','Yes')");

但我得到一个异常“表达式包含未定义的函数调用 DateDiff()。”

(请注意,我总是想要获取该行,无论它是否过期)

如何将列的文本设置为正确的形式?

I have the column DateTimeExpired, and I would like to create another column called "Expired" which will show "Yes" or "No" according to the expiration date - "Yes" if the date has already passed.
I wrote this:

DataColumn colExpirationDate = new DataColumn("DateTimeExpired", typeof(DateTime));
DataColumn colExpired = new DataColumn("Expired", typeof(string), "IIF(DateDiff(DateTimeExpired, date())>= 0,'No','Yes')");

But I get an exception "The expression contains undefined function call DateDiff()."

(please note that I always want to get the row, no matter if it's expired or not)

How do I set the column's text to the correct form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴意少年 2024-09-04 19:02:50

正如 MA Hanin 在评论中指出的那样,看起来 DateDiff 不能在 数据列表达式。您可以尝试将计算列构建到基础表中(如果您使用的是 MS Sql 或类似的)

编辑:
没有函数可以获取“今天”,但假设您添加的 DataColumn 只会存在几个小时,您可以将今天的日期构建为常量,然后使用比较运算符而不是 DateDiff

尝试这样做:

DataColumn colExpirationDate = new DataColumn("DateTimeExpired", typeof(DateTime));
DataColumn colExpired = new DataColumn("Expired", typeof(string),
    String.Format("IIF(DateTimeExpired > #{0}#,'No','Yes')",
    DateTime.Now.ToString("dd/MMM/yyyy")));

注意这将仅当您的 DataColumn 仅在内存中保留少于一天时才有效。

As M.A Hanin pointed out in his comment, it looks like DateDiff cannot be used in DataColumn Expressions. You could try building the calculated column into the underlying table instead (if you are using MS Sql or similar)

edit:
There is no function to get 'today', but assuming that the DataColumn you are adding will only exist for a few hours, you could build in todays date as a constant, and then use comparison operators instead of DateDiff

Try this:

DataColumn colExpirationDate = new DataColumn("DateTimeExpired", typeof(DateTime));
DataColumn colExpired = new DataColumn("Expired", typeof(string),
    String.Format("IIF(DateTimeExpired > #{0}#,'No','Yes')",
    DateTime.Now.ToString("dd/MMM/yyyy")));

Note this will only work if your DataColumn is only retained in memory for less than a day.

微凉 2024-09-04 19:02:49

您所要做的就是使用 Date.ToOADate 将两个日期列转换为双精度数,然后相减。

我使用 ToOADate 填充每个日期列的隐藏列。

All you have to do is convert your two date columns using Date.ToOADate to doubles then subtract.

I use ToOADate to populate hidden columns for each date column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文