c# - How to filter a DateTime column by Date -


my database sql server 2008 , have query returns details filtered date. in close of query have this

where (convert (date, attendance.in_time) = @indate) 

note : attendance.in_time datetime column

here i'm trying date part in_time , compare @indate parameter in .net.

my problem is, in .net cant have data part of datetime. 1 option convert string follows

var indate = intime.date.tostring("d"); 

but problem date , string cannot compared in sql query?

can provide solution?

edit : requested in comments i'm showing full query here ...

 public list<iattendance> showattendance(datetime indate, string pid, list<iattendance> list)         {             string selectstatement = "select employee.emp_id, employee.initials + ' ' + employee.surname name, attendance.in_time, attendance.out_time, attendance.shift "+                                      "from attendance inner join employee on attendance.eid = employee.emp_id "+                                      "where (convert (date, attendance.in_time) = @indate) , (attendance.pid = @pid) ";  //  } 

you change sql query exploit between clause

 public list<iattendance> showattendance(datetime indate, string pid, list<iattendance> list)  {     string selectstatement = "select employee.emp_id, employee.initials + ' '" +                               "employee.surname name, attendance.in_time, " +                               "attendance.out_time, attendance.shift " +                              "from attendance inner join employee " +                               "on attendance.eid = employee.emp_id " +                              "where (attendance.in_time between @indate , @enddate) " +                               "and attendance.pid = @pid";     ......  } 

and pass 2 parameters:

cmd.parameters.addwithvalue("@indate", indate.date); cmd.parameters.addwithvalue("@enddate", indate.date.addminutes(1439)); ..... 

in way, returned records belogs same day without need of complex conversions on sql server side.

as pointed out in comments below, if there values stored after 23.59 use addseconds(86399) instead of addminutes(1439) enclose these values. , if need milliseconds precision due nature of datetime sql type then

cmd.parameters.addwithvalue("@enddate", indate.date.addseconds(86399).addmilliseconds(999)); 

again, due comments below. best option use

"where (attendance.in_time >= @indate , attendance.in_time < @enddate) "

with parameters defined

cmd.parameters.addwithvalue("@indate", indate.date); cmd.parameters.addwithvalue("@enddate", indate.date.addday(1)); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -