SQL 'DATE' 不是可识别的内置函数名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20973272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:40:09  来源:igfitidea点击:

'DATE' is not a recognized built-in function name

sqlsql-serversql-server-2008sql-server-2012ssms

提问by sajjad

I wish to find all records of the current day. I have a field Date of type DATE. I am getting error on sql server

我希望找到当天的所有记录。我有一个日期类型的字段日期。我在 sql server 上遇到错误

'DATE' is not a recognized built-in function name.

on this line

在这条线上

(DATE(EnterDate) = CURDATE() )

回答by D Stanley

As the error states, there is no DATEfunctionin SQL Server 2008 or 2012 (you tagged both so I'm not sure which you're targeting). You can, however, cast to a datetype in SQL Server 2008 and above:

正如错误所述,SQL Server 2008 或 2012 中没有任何DATE功能(您标记了两者,所以我不确定您的目标是哪个)。但是,您可以转换为dateSQL Server 2008 及更高版本中的类型:

WHERE EnterDate = CONVERT(date,GETDATE())

Note that there's no CURDATEfunction either, so I've translated that to GETDATE()

请注意,也没有CURDATE函数,所以我已将其翻译为GETDATE()

回答by M.Ali

Use the following condition in your where cluase

在您的 where 条款中使用以下条件

WHERE CAST(DateColumn AS DATE) = CAST(GETDATE() AS DATE)
              ^------------ Your Column Name with `Date` or 'DateTime'  data type

CURDATE()is a mysql function, In Sql-Server we have GETDATE()function to get current date and time.

CURDATE()是一个mysql函数,在Sql-Server中我们有GETDATE()获取当前日期和时间的函数。

回答by sajjad

More efficient one is

更有效的一种是

WHERE EnterDate > DATEADD(dd, -1, DATEDIFF(dd, 0, GETDATE()))

Thanks @D Stanley @marc_S and @Mihai

谢谢@D Stanley @marc_S 和@Mihai

回答by Pradeep Samaranayake

This is not the exact answer but example how to trim the time part from the date time variable

这不是确切的答案,而是如何从日期时间变量中修剪时间部分的示例

CONVERT(VARCHAR(10),date_manufactured,10) =CONVERT(VARCHAR(10),@startdate,10))

CONVERT(VARCHAR(10),date_manufactured,10) =CONVERT(VARCHAR(10),@startdate,10))

回答by sajjad

Finally I get it done by
WHERE EnterDate > Convert(DATETIME,Convert(varchar,DATEADD(DAY,0,GETDATE()),101))

最后我通过
WHERE EnterDate > Convert(DATETIME,Convert(varchar,DATEADD(DAY,0,GETDATE()),101)) 完成它