Dropdown variable for interval with custom list

How do I make this work?

and id > id_from_timestamp(NOW() AT TIME ZONE 'utc' - INTERVAL {{ time }}, true)

The list looks like:

1 day, Last Day
1 week, Last Week
1 month, Last Month
3 months, Last 3 Months
1 year, Last Year

I tried all different combinations with and without quotes.

Hello there,

I tried {{ time }}::interval and got it to work, could you try that one?

No luck, I get the following error:

ERROR: syntax error at or near "$3" Position: 356

If I replace {{ time }}::interval with '1 day' it works, so it isn't the query.

Just to double check, you are removing the INTERVAL before it, right?
Something like this:
“ id > id_from_timestamp(NOW() AT TIME ZONE 'utc' - {{ time }}::interval , true)”

If that doesn’t work, please send us the Metabase version and DB engine just to be sure.

1 Like

That worked, thanks!

I don't get how you knew to use {{ time }}::interval.

Can you explain what is happening here?

When executing the query in postgres, SQL variables are sent as JDBC parameters on a parameterized query, so it looks like SELECT INTERVAL $1, and 1 day is sent as the first parameter. Those behave differently from just using a literal like SELECT INTERVAL '1 day'.
I saw that they didn't work and searched for "Parameterized interval postgres" and found this answer that showed the ::interval trick :smiley:

1 Like

Thanks for looking into it, I've been trying to use this for a while to setup a drop down for time ranges I use a lot of queries where I was just using a numbers field.

1 Like