简体   繁体   中英

DateTime Comparison in SQL Server

I want to compare DateTime in SQL Server. But I am using Varchar datatype in Table. So I am using CONVERT in SQL.

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 .

Query After writing DateTime And UserId which I get from SqlCommand.

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. But I am using Varchar datatype in Table.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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