Handle empty variables

I have a MYSQL query that is referenced to a template variable. When the variable returns null, the mysql query fails since the IN clause contains an empty list. How do we handle such a scenario.

1 Like

You can write CASE statement in your query .

Actually you cannot use CASE - Grafana does direct substitution on template variables. If no values are returned from variables, it ll be blank. So CASE or COALESCE can’t be used in this situation.

You can resolve this issue using the following
WHERE id IN (${id:sqlstring}+'');

Also ran into this issue when using Loki, When I wanted no results returned when nothing was selected
|~ "${id:regex}|EMPTYDEFAULTVALUE"

It would be nice to add a filter to provide a default value

1 Like

The WHERE id IN (${id:sqlstring}+''); solution above started to fail me in certain situations.
A much better solution is WHERE FIND_IN_SET(id, '${id:csv}')

1 Like