如何在SQL Server中无需时间获取日期 [英] How to fetch only date without time in SQL server
问题描述
我已经检索了当前的日期明智数据...所以请快速帮助我
我尝试了什么: < br $>
string strdat = System.DateTime.Now.ToString(yyyy-MM-dd);
SqlCommand cmd = new SqlCommand(选择(UserId)为[Emp Code],(EmployeeName)为[Employee Name],CAST((LogDate)为[Date]),(Direction)为[IN / OUT],来自DeviceLogs,员工,其中DeviceLogs.UserId = Employees.EmployeeCode)和LogDate = @ date,conaccess);
cmd.Parameters.AddWithValue(@ date,strdat);
不要将日期作为字符串传递:将它们作为DateTime对象发送。剥离DateTime的时间很简单:
cmd.Parameters.AddWithValue( @ date,DateTime.Now.Date);
这会将时间组件重置为午夜。
然后执行比较:
WHERE DeviceLogs.UserId = Employees.EmployeeCode) AND CAST(LogDate AS DATE )= @date它应该全部工作。
你好,
这不是一个更好的方法,因为你是传递System.DateTime.Now.ToString(yyyy-MM-dd),时间元素设置为午夜。
在sqlserver中仅比较datepart施展它然后比较它
像这样改变你的查询
CAST(LogDate as DATE)= @ date
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);
Don't pass dates as strings: send them as DateTime objects. To strip the time off a DateTime is easy:
cmd.Parameters.AddWithValue("@date", DateTime.Now.Date);This "resets" the time component to midnight.
Then do the comparison:
WHERE DeviceLogs.UserId = Employees.EmployeeCode) AND CAST(LogDate AS DATE) = @dateand it should all work.
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
这篇关于如何在SQL Server中无需时间获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!