DATEADD(day, -7, GETDATE()) - Does it take time into account?

nak5120 picture nak5120 · Jul 19, 2017 · Viewed 8.6k times · Source

I am writing a SQL query in Aginity through Amazon Redshift to extract the last 7 days of data. The Date column which I am calling is in the variable type:

DATE

An example output is this:

5/30/2017 0:00

When I call the below function, does it matter what time of day I run this query or will it always take the full day's worth of data?

WHERE Date >= DATEADD(day,-7, GETDATE())

Answer

Gordon Linoff picture Gordon Linoff · Jul 19, 2017

Yes, this includes the current time component when subtracting 7 days. To get rid of that, convert to a date:

WHERE Date >= CAST(DATEADD(day, -7, GETDATE()) as DATE)