Using percent signs with template variables in MySQL datasource

I am using a MySQL datasource, I have a template variable, and in the query I want to use the template variable in a WHERE clause with the LIKE operator.

For example: WHERE Summary LIKE %$variable%

But this doesn’t work, the resulting SQL is: WHERE Summary LIKE %"variable"%

I can work around it crudely by adding the % to the custom variables, so instead of say “server1” I put “%server1%”. Is there any better way to deal with this? The problem seems to be that Grafana automatically adds the quotes around the variable but only the variable. It would be nice is there was maybe an escape character that told Grafana not to automatically add quotes.

1 Like

Template variables are quoted right now, we are looking into changing that

Awesome, thanks! Should I open a Github issue for tracking or is there already one?

Hi,
i’ve just updated to Grafana 4.6 beta 2 and trying to use SQL Queries as template variables in SQL Queries.
There is still the problem of the quoted template variables!
When it’s going to be fixed? Is there a work around?
Edit: i found a solution by calling a stored proc in the variables query with the other variable(string) as in parameter.
in the stored proc i can concat the string to a query and execute.

Hi - I have a very similar problem

I try to send a filter criteria for the mySQL statement via URL:

URL:
http://localhost:3000/dashboard/db/foo?orgId=1&var-SWSC=abc

SQL statement:
SELECT UNIX_TIMESTAMP(metric_time) as time_sec,
metric_value as value,
metric_key as metric
FROM COBOL_COMP_TREND
WHERE $__timeFilter(metric_time)
AND SWSC= '%var-SWSC%'
ORDER BY metric_time ASC

sql used by grafana:
rawSql:"SELECT UNIX_TIMESTAMP(metric_time) as time_sec, metric_value as value, metric_key as metric FROM COBOL_COMP_TREND WHERE $__timeFilter(metric_time) AND SWSC= ‘%var-SWSC%’ ORDER BY metric_time ASC"
format:“time_series”

the same happens with double quotes. Any help?

come guys, do not you have any idea?

Hi…maybe it’s late…but this is how a got around it last week when i encountered same problem:

…LIKE concat(’%’,$year,’-’,$month,’%’)

$year and $month are variables selected from template. Though in grafana 5.0 they corrected these issues as i read from relese notes…

Cheers!

Many thanks - in the meantime I changes to v5