Home > Blockchain >  current_date in redshift exclude today's date when i am using with between command
current_date in redshift exclude today's date when i am using with between command

Time:01-07

I want to query data for last 30 days including today from redshift table. below is my query.

my date_column's type is 'timestamp without timezone'

select * 
from mytable  
WHERE  date_column BETWEEN current_date - INTERVAL '30 day' AND current_date 
order by date_column desc;

It gives the result for 30 days. But it doesn't include today's result.

I want to query for 30 days result including today's result also.

CodePudding user response:

If it's a timestamp don't use between as it also compares the time part. Use a range query:

where date_column >= current_date - interval '30 day'
  and date_column < current_date   interval '1 day'

Note that the upper bound is using < together with "tomorrow"

With Postgres this could be simplified to

where date_column >= current_date - 30
  and date_column < current_date   1

but Redshift isn't Postgres and I don't know if that would work there.

  • Related