查询如何比较表中今天的日期和日期字段,比较是否为"=". [英] Query for How to compare today date and date field in table and comparision is '='.

查看:73
本文介绍了查询如何比较表中今天的日期和日期字段,比较是否为"=".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


这篇关于查询如何比较表中今天的日期和日期字段,比较是否为"=".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆