Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have retrieve current date wise data not time wise...so please help me fast

What I have tried:

string strdat = System.DateTime.Now.ToString("yyyy-MM-dd");

SqlCommand cmd = new SqlCommand("Select (UserId) as [Emp Code],(EmployeeName) as [Employee Name],CAST((LogDate) as [Date]), (Direction) as [IN/OUT] from DeviceLogs,Employees where DeviceLogs.UserId = Employees.EmployeeCode) and LogDate=@date ", conaccess);
cmd.Parameters.AddWithValue("@date" , strdat);
Posted
Updated 16-May-16 21:42pm

Don't pass dates as strings: send them as DateTime objects. To strip the time off a DateTime is easy:
C#
cmd.Parameters.AddWithValue("@date", DateTime.Now.Date);
This "resets" the time component to midnight.
Then do the comparison:
SQL
WHERE DeviceLogs.UserId = Employees.EmployeeCode) AND CAST(LogDate AS DATE) = @date
and it should all work.
 
Share this answer
 
Hello ,
That will not a better approach as you are passing System.DateTime.Now.ToString("yyyy-MM-dd") , the time element is set to midnight .

In sqlserver to compare only datepart you first cast it and then compare it
change your query like this way
CAST(LogDate as DATE)=@date
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900