查询如何比较表中今天的日期和日期字段,比较是否为"=".

查询如何比较表中今天的日期和日期字段,比较是否为

问题描述:

查询如何比较表比较中的今天日期和日期字段为"=".

Query for How to compare today date and date field in table comparision is ''=''.

最安全的方法是使用DatePart:
The safest way is to use DatePart:
WHERE DATEPART(year, demodate)=DATEPART(year, GETDATE()) AND DATEPART(dy, demodate)=DATEPART(dy, GETDATE())

这仅返回年份和年份相同的记录-请注意,如果您应一次读取GETDATE并将其存储,否则可能会导致新年前后的间歇性错误!

This returns only records where the year and day of year are the same - be aware that if you should read GETDATE once, and store it, as otherwise is may cause intermittent errors around new year!


对于SQL Server尝试此操作,

Try this for SQL server,

SELECT yourField FROM yourTable WHERE yourDateField = GETDATE();



或对于MySql



or for MySql

SELECT yourField FROM yourTable WHERE yourDateField = NOW();



希望对您有帮助



Hope this helps


如果您使用的是SQL2008,则可以转换为Date数据类型
例如在AdventureWorks数据库中
If you are using SQL2008, you can convert to the Date datatype
For example in the AdventureWorks Database
SELECT  *
FROM    Sales.SalesOrderHeader
WHERE   CAST(OrderDate AS DATE) = CAST(GETDATE() AS DATE)


J