I am trying to write a query where the clause is when the start date for an employee is todays date.
select * from tbl_employees
where Startdate = getdate()
The issue is that Startdate is '2014-12-09 00:00:00.000'
and the function getdate
is coming back with the date and time like '2014-12-09 08:25:16.013'
How can I write a query that only consider's the date?
You want just the date portion. The easy way is:
select *
from tbl_employees
where cast(Startdate as date) = cast(getdate() as date);
However, if you want to use an index, it is best not to have the column in a function call. So, this is better:
where (StartDate >= cast(getdate() as date) and StartDate < cast(getdate() + 1 as date))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With