如何在SQL 2008中的where子句中仅与日期进行比较并忽略时间 [英] How to compare with only dates and ignore time in where clause in SQL 2008

查看:71
本文介绍了如何在SQL 2008中的where子句中仅与日期进行比较并忽略时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的列名为Expiry_Date,其中包含日期和时间在table1中的值。

我发送的是使用c#.net从前端开始的日期值,



如果条件为真,我会编写查询以获取一些信息。

like :



从table1选择USER_NAME,USER_EMAIL,其中Expiry_Date ='2016-03-12';



当我执行此操作时,我没有得到任何结果。实际上表格中包含该日期的数据,因为没有时间在供应日期。它与Expiry_Date不匹配。



所以请告诉我们如何在不从前端传递时间的情况下这样做。



先谢谢。



我尝试过:



例如:在表1中,Expiry_Date的值为:2016-03-11 23:59:59.000



从table1中选择USER_NAME,USER_EMAIL,其中Expiry_Date = '2016-03-12';

Hi,

I have column named "Expiry_Date" having the values with date and time in table1.
I am sending a date value from the front end using c#.net,

I write the query to get some information if where condition is true.
like:

select USER_NAME,USER_EMAIL from table1 where Expiry_Date='2016-03-12';

when I execute this,Iam not getting any result. Actually table have the data for this date, due to not having time part in supplied date. its not matching with Expiry_Date.

So plz let me know how can we do this without passing time from front end.

Thanks in Advance.

What I have tried:

For ex: In table1, Expiry_Date having the value is: 2016-03-11 23:59:59.000

select USER_NAME,USER_EMAIL from table1 where Expiry_Date='2016-03-12';

推荐答案

CAST it:

CAST it:
SELECT USER_NAME,USER_EMAIL FROM table1 WHERE CAST(Expiry_Date AS DATE) = '2016-03-12'



将DATETIME作为DATE转换剥离时间部分,并将其重置为午夜。


Casting a DATETIME as a DATE strips off the time part, and resets it to midnight.


这篇关于如何在SQL 2008中的where子句中仅与日期进行比较并忽略时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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