Is there a way to use the Now()
function in SQL to select values with today's date?
I was under the impression Now()
would contain the time as well as date, but today's date would have the time set to 00:00:00
and therefore this would never match?
Building on the previous answers, please note an important point, you also need to manipulate your table column to ensure it does not contain the time fragment of the datetime datatype.
Below is a small sample script demonstrating the above:
I hope this helps.
EDIT:
Following @dwurf comment (thanks) about the effect the above example may have on performance, I would like to suggest the following instead. We create a date range between today at midnight (start of day) and the last millisecond of the day (SQL server count up to .997, that's why I'm reducing 3 milliseconds). In this manner we avoid manipulating the left side and avoid the performance impact.
where created_date between CURRENT_TIMESTAMP-180 and CURRENT_TIMESTAMP
There is no native Now() function in SQL Server so you should use:
you can get day, month and year separately by doing:
if you are on sql server 2008, there is the DATE date time which has only the date part, not the time: