mezamorphic mezamorphic - 1 year ago 73
SQL Question

Comparing results with today's date?

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?

Answer Source

There is no native Now() function in SQL Server so you should use:

select GETDATE() --2012-05-01 10:14:13.403

you can get day, month and year separately by doing:

select DAY(getdate())  --1
select month(getdate())  --5
select year(getdate()) --2012

if you are on sql server 2008, there is the DATE date time which has only the date part, not the time:

select cast (GETDATE() as DATE) --2012-05-01
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download