# Extract last character of a variable for a SQL filter

**URL:** <https://community.grafana.com/t/extract-last-character-of-a-variable-for-a-sql-filter/68139>\
**Category:** Dashboards\
**Tags:** regex, flux, mssql\
**Created:** [July 4, 2022, 6:36am UTC](https://community.grafana.com/t/extract-last-character-of-a-variable-for-a-sql-filter/68139 "2022-07-04T06:36:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmu](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jmu](https://community.grafana.com/u/jmu)\
**Post date:** [July 4, 2022, 6:36am UTC](https://community.grafana.com/t/extract-last-character-of-a-variable-for-a-sql-filter/68139/1 "2022-07-04T06:36:42Z")

</div>

Hi,

I have a variable called “Maquina” that has values: S1,S2,S3…  
We have a graph that has a SQL source where the filter is going to be this “Maquina” variable. However, we want only the number: 1,2,3. How do we extract the last character of the variable.

I tried multiple formulas but non worked.

> WHERE  
> ($\_\_timeFilter(fecha\_inicio) or $\_\_timeFilter(fecha\_fin)) AND CodMaqui = strings.substring($Maquina, start:2, end:2)

Thanks beforehand

---

<div class="post-metadata">

**Author:** ![usman.ahmad](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/usman.ahmad/32/29706_2.png) [@usman.ahmad](https://community.grafana.com/u/usman.ahmad)\
**Post date:** [July 21, 2022, 9:47am UTC](https://community.grafana.com/t/extract-last-character-of-a-variable-for-a-sql-filter/68139/2 "2022-07-21T09:47:11Z")

</div>

Hi @jmu

I am not sure but if I understood you correctly, those variables are defined in the Grafana dashboard ?

If that is the case, then simply use the regex when setting up this variable. Here is a reference link to the documentation.

> **[Filter variables with regex](https://grafana.com/docs/grafana/latest/variables/filter-variables-with-regex/)**
>
> Filter variables with regex Using the Regex Query option, you filter the list of options returned by the variable query or modify the options returned. …

You probably have to play around with it to make it working but it should give you your desired results.

---

<div class="post-metadata">

**Author:** ![alexandrearmand](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@alexandrearmand](https://community.grafana.com/u/alexandrearmand)\
**Post date:** [July 21, 2022, 12:06pm UTC](https://community.grafana.com/t/extract-last-character-of-a-variable-for-a-sql-filter/68139/3 "2022-07-21T12:06:34Z")

</div>

> [@jmu](#):
>
> WHERE  
> ($\_\_timeFilter(fecha\_inicio) or $\_\_timeFilter(fecha\_fin)) AND CodMaqui = strings.substring($Maquina, start:2, end:2)

i believe you have to make your column transformation in the select not in where,  
like that :

select right($Maquina, length ) as char\_maquina , CodMaqui from table  
where  
($\_\_timeFilter(fecha\_inicio) or $\_\_timeFilter(fecha\_fin)) AND CodMaqui = right($Maquina, length )

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [July 21, 2022, 5:17pm UTC](https://community.grafana.com/t/extract-last-character-of-a-variable-for-a-sql-filter/68139/4 "2022-07-21T17:17:13Z")

</div>

is this microsoft sql server? why does the variable Maquina have the values with S in the first place? Fix it there would be my recommendation. But we would need to see what it is that builds the Maquina query.

you could do this but does not address the root issue.

```auto
replace('S1,S2,S3','S','')

```

there are great pitfalls in string manipulation that **will** (not might) come to bite you later on

---

<div class="post-metadata">

**Author:** ![jmu](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jmu](https://community.grafana.com/u/jmu)\
**Post date:** [July 22, 2022, 8:21am UTC](https://community.grafana.com/t/extract-last-character-of-a-variable-for-a-sql-filter/68139/5 "2022-07-22T08:21:28Z")

</div>

Finally I get it what i want.  
This is the code:

> CodMaqui = SUBSTRING(${Maquina:sqlstring},2,1)

This way, the variable that has the value “S1” for example, i only get the last character which is the 1 and compare it in the database.
