简体   繁体   English

SQL Server中的DateTime比较

[英]DateTime Comparison in SQL Server

I want to compare DateTime in SQL Server. 我想在SQL Server中比较DateTime But I am using Varchar datatype in Table. 但是我在表中使用Varchar数据类型。 So I am using CONVERT in SQL. 所以我在SQL中使用CONVERT

My Query : 我的查询:

Select io.* from tblData as io INNER JOIN tblInterest as ui 
ON ui.SubCatId=io.SubCatId 
Where ui.UserId=@UserId 
AND (CONVERT(DATETIME,io.Start,103 <=CONVERT(DATETIME,@Start,103) 
AND CONVERT(DATETIME,io.END,103)>=CONVERT(DATETIME,@End,103))

Now, Issue is that If I am firing query directly then It is showing me data but when It executes in C# then I get black DataTable . 现在,问题是,如果我直接触发查询,那么它将向我显示数据,但是当它在C#中执行时,我会得到黑色的DataTable

Query After writing DateTime And UserId which I get from SqlCommand. 写完从SqlCommand获取的DateTime和UserId后进行查询。

Select io.* from tblData as io INNER JOIN tblInterest as ui 
ON ui.SubCatId=io.SubCatId 
Where ui.UserId=1 
AND (CONVERT(DATETIME,io.Start,103)<=CONVERT(DATETIME,'15-04-2014 23:13:30',103) 
AND CONVERT(DATETIME,io.END,103)>=CONVERT(DATETIME,'15-04-2014 23:13:30',103))

What can be the issue ? 可能是什么问题?

I want to compare DateTime in SQL Server. 我想在SQL Server中比较DateTime。 But I am using Varchar datatype in Table. 但是我在表中使用Varchar数据类型。

Right here is the error. 错误就在这里。 STOP THAT. 别搞了。 You can not do that efficiently, it is a typical beginner mistake and you better stop that at the root cause, which is a non-standard way of handling datetime. 您不能有效地做到这一点,这是一个典型的初学者错误,您最好从根本上阻止它,这是处理日期时间的非标准方法。 Then suddenly everything else turns out to be super easy. 突然之间,其他所有事情都变得异常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM