Skip to content
Advertisement

Multiselect Widget to SQL Query

I am trying to make a SQL Query to a Database with the inputs obtained from a Multiselect Widget.

The input of the Widget just mentioned is:

{{Inputs.selectedDisplayValues}} = January, February #No parentheses or quotes

In a database I want to make a Query in the Month column, requesting that it only give me the information of the months that are obtained from the widget. But this causes a Syntax Error.

The SQL that isn’t working is:

Select month, status FROM Database WHERE mes IN ('{{Inputs.selectedDisplayValues}}')

Equivalent to

Select month, status FROM Database WHERE mes IN ('January, February')

The desired result is to fix the array (‘January, February’) to finally be (‘January’, ‘February’)

Advertisement

Answer

Since string splitting differs among various db vendors, you should specify your db vendor to target help more precisely.

In Postgres, the IN clause could be rewritten to:

... IN (select m from regexp_split_to_table(' January, February ','s*,s*|^s*|s*$') as u(m));

The regexp trims whitespace. In case when no unnecessary whitespace was guaranteed, string_to_array would suffice.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement