Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select where startdate is today's date

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?

like image 635
user2296463 Avatar asked Dec 20 '22 08:12

user2296463


1 Answers

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))
like image 120
Gordon Linoff Avatar answered Dec 21 '22 22:12

Gordon Linoff