T-SQL:在WHERE中使用{fn NOW()}

时间:2021-10-21 11:49:22

I was reviewing some SQL queries and I saw a select statement that looked like this

我正在审查一些SQL查询,我看到了一个看起来像这样的select语句

SELECT *
FROM dbo.mytable
WHERE (dbo.mytable.[Date] < { fn NOW() })

What is the purpose of using a WHERE statement like this?

使用像这样的WHERE语句的目的是什么?

Wouldn't be easier to use a simple GETDATE()?

使用简单的GETDATE()会不会更容易?

1 个解决方案

#1


24  

http://www.sqlservercentral.com/Forums/Topic183904-8-1.aspx

http://www.sqlservercentral.com/Forums/Topic183904-8-1.aspx

GETDATE() is a T-SQL specific function which returns the current system date and time. The SQL standard equivalent is CURRENT_TIMESTAMP which is applicable in T-SQL as well. The {fn Now()} is an ODBC canonical function which can be used in T-SQL since the OLE DB provider for SQL Server supports them. There are no notable performance difference between these though. You can also use canonical format like :

GETDATE()是一个特定于T-SQL的函数,它返回当前系统的日期和时间。 SQL标准等价物是CURRENT_TIMESTAMP,它也适用于T-SQL。 {fn Now()}是一个ODBC规范函数,可以在T-SQL中使用,因为SQL Server的OLE DB提供程序支持它们。但是这些之间没有明显的性能差异。您还可以使用规范格式,如:

SELECT {fn CURRENT_TIMESTAMP()} AS "date & time",
       {fn CURRENT_DATE()} AS "date only",
       {fn CURRENT_TIME()} AS "time only" ; 

#1


24  

http://www.sqlservercentral.com/Forums/Topic183904-8-1.aspx

http://www.sqlservercentral.com/Forums/Topic183904-8-1.aspx

GETDATE() is a T-SQL specific function which returns the current system date and time. The SQL standard equivalent is CURRENT_TIMESTAMP which is applicable in T-SQL as well. The {fn Now()} is an ODBC canonical function which can be used in T-SQL since the OLE DB provider for SQL Server supports them. There are no notable performance difference between these though. You can also use canonical format like :

GETDATE()是一个特定于T-SQL的函数,它返回当前系统的日期和时间。 SQL标准等价物是CURRENT_TIMESTAMP,它也适用于T-SQL。 {fn Now()}是一个ODBC规范函数,可以在T-SQL中使用,因为SQL Server的OLE DB提供程序支持它们。但是这些之间没有明显的性能差异。您还可以使用规范格式,如:

SELECT {fn CURRENT_TIMESTAMP()} AS "date & time",
       {fn CURRENT_DATE()} AS "date only",
       {fn CURRENT_TIME()} AS "time only" ;