SQL Server 日期函数

发布于 2024-07-13 11:01:03 字数 2012 浏览 9 评论 0原文

当我将其添加到我的 where 子句中时:

DueDate <= getDate() AND DueDate IS NOT null

我得到一个不错的小失败,上面写着: 无法创建子项:System.Reflection.TargetInitationException:调用目标已引发异常。 ---> System.Data.EvaluateException:表达式包含未定义的函数调用 getDate()。

知道我做错了什么吗?


更新 使用中的代码:

private string getFilter(int mode) {
    String filter = "";
    if ((ALL_WORK_ORDERS & mode) == 0) {
        if ((mode & OUTSTANDING_WORK_ORDERS) == OUTSTANDING_WORK_ORDERS) {
            filter += "DueDate <= getDate() AND DueDate IS NOT null OR";
        }
        if ((mode & COMPLETED_WORK_ORDERS) == COMPLETED_WORK_ORDERS) {
            filter += " FinishedDate IS NOT  null";
        }
    }

    filter = filter.Trim();
    if (filter.EndsWith("OR")) {
        filter = filter.Remove(filter.Length - 2);
    }

    return filter;
}

在这里使用它:

tblWorkOrderBindingSource.Filter = getFilter(mode);

在表中,DueDate 是日期时间类型。

旁注: 我可以

SELECT [ID]
      ,[WorkDesc]
      ,[DueDate]
  FROM [RentalEase].[dbo].[tblWorkOrder]
WHERE [DueDate] <= getDate() AND [DueDate] IS NOT null

在 MS SQL Server Management Studio Express 中运行,没有任何问题。


最终解决方案

    private string getFilter(int mode) {
        String filter = "";
        if ((ALL_WORK_ORDERS & mode) == 0) {
            if ((mode & OUTSTANDING_WORK_ORDERS) == OUTSTANDING_WORK_ORDERS) {
                filter += "DueDate <= #" + DateTime.Now.ToShortDateString() + "# AND DueDate IS NOT null  AND FinishedDate IS null OR";
            }
            if ((mode & COMPLETED_WORK_ORDERS) == COMPLETED_WORK_ORDERS) {
                filter += " FinishedDate IS NOT null";
            }
        }

        filter = filter.Trim();
        if (filter.EndsWith("OR")) {
            filter = filter.Remove(filter.Length - 2);
        }

        return filter;
    }

When I add this to my where clause:

DueDate <= getDate() AND DueDate IS NOT null

I get a nice little failure that says:
Could not create child: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.EvaluateException: The expression contains undefined function call getDate().

Any idea what I'm doing wrong?


Update
Code in use:

private string getFilter(int mode) {
    String filter = "";
    if ((ALL_WORK_ORDERS & mode) == 0) {
        if ((mode & OUTSTANDING_WORK_ORDERS) == OUTSTANDING_WORK_ORDERS) {
            filter += "DueDate <= getDate() AND DueDate IS NOT null OR";
        }
        if ((mode & COMPLETED_WORK_ORDERS) == COMPLETED_WORK_ORDERS) {
            filter += " FinishedDate IS NOT  null";
        }
    }

    filter = filter.Trim();
    if (filter.EndsWith("OR")) {
        filter = filter.Remove(filter.Length - 2);
    }

    return filter;
}

And it is getting used here:

tblWorkOrderBindingSource.Filter = getFilter(mode);

In the table, DueDate is type datetime.

Side note:
I can run

SELECT [ID]
      ,[WorkDesc]
      ,[DueDate]
  FROM [RentalEase].[dbo].[tblWorkOrder]
WHERE [DueDate] <= getDate() AND [DueDate] IS NOT null

in the MS SQL Server Management Studio Express without a problem.


Final Solution

    private string getFilter(int mode) {
        String filter = "";
        if ((ALL_WORK_ORDERS & mode) == 0) {
            if ((mode & OUTSTANDING_WORK_ORDERS) == OUTSTANDING_WORK_ORDERS) {
                filter += "DueDate <= #" + DateTime.Now.ToShortDateString() + "# AND DueDate IS NOT null  AND FinishedDate IS null OR";
            }
            if ((mode & COMPLETED_WORK_ORDERS) == COMPLETED_WORK_ORDERS) {
                filter += " FinishedDate IS NOT null";
            }
        }

        filter = filter.Trim();
        if (filter.EndsWith("OR")) {
            filter = filter.Remove(filter.Length - 2);
        }

        return filter;
    }

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

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

发布评论

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

评论(5

毅然前行 2024-07-20 11:01:03

这个异常很清楚地表明这里出了什么问题。 您指定的方法 getdate 不能在过滤表达式中使用。 请参阅有关 DataColumn 类的 Expression 属性的文档,了解哪些内容有效,哪些内容无效:

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx

也就是说,您需要连接 getdate 而不是 getdate将日期添加到过滤器中(调用 DateTime 结构上的静态 Now 属性来获取当前日期)。 上面的链接将向您展示如何正确设置日期格式(您不能只按照凯利的答案中所示的方式进行操作,尽管该答案大部分都是如此)。

The exception is pretty clear on what is wrong here. You are specifying a method, getdate, which can't be used in a filter expression. See the documentation on the Expression property on the DataColumn class for what is valid and what is not:

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx

That being said, instead of getdate, you need to concatenate the date into the filter (call the static Now property on the DateTime structure to get the current date). The link above will show you how to format dates properly (you can't just do it as indicated in Kelly's answer, although that answer is most of the way there).

樱娆 2024-07-20 11:01:03

你有没有尝试过

filter += "DueDate <= #" + DateTime.Now + "# AND DueDate is not Null"

have you tried

filter += "DueDate <= #" + DateTime.Now + "# AND DueDate is not Null"
像极了他 2024-07-20 11:01:03

System.Reflection.TargetInitationException

此消息不是来自 Sql Server。 我认为您实际上是在该字符串中编写 C# 并在运行时针对 .net 类型(而不是表定义)对其进行编译/解释。

另外,SQLServer 2005 在 || 方面存在问题。 和&&...

SELECT CASE WHEN 1=1 && 3=3 THEN 1 ELSE 0 END
--Incorrect syntax near '&'.

SELECT CASE WHEN 1=1 || 3=0 THEN 1 ELSE 0 END
--Incorrect syntax near '|'.

System.Reflection.TargetInvocationException

This message did not come from Sql Server. Methinks you are actually writing C# in that string and getting it compiled/interpretted at runtime against .net types (instead of table defs).

Also, SQLServer 2005 has trouble with || and &&...

SELECT CASE WHEN 1=1 && 3=3 THEN 1 ELSE 0 END
--Incorrect syntax near '&'.

SELECT CASE WHEN 1=1 || 3=0 THEN 1 ELSE 0 END
--Incorrect syntax near '|'.
你的往事 2024-07-20 11:01:03

那应该是 T-SQL 吗? 尝试这个

DueDate <= getDate() 
AND  DueDate IS NOT null

Is that supposed to be T-SQL? try this

DueDate <= getDate() 
AND  DueDate IS NOT null
烟火散人牵绊 2024-07-20 11:01:03

如果这应该是 sql,那么 "||" 不是 "OR" 的符号,并且 "&&" 也不是AND 的符号。

filter += "DueDate <= GetDate() AND DueDate is not NULL OR ";

If this is supposed to be sql then "||" is not the symbol for "OR" and "&&" is not the symbol for AND.

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