Add AND to sql query if value from input is not null?

Hi,

I want to have a query that displays in a table on retool, I want to be able to search certain fields but only if a value has been entered into the search box.

So I want it to by default have SELECT * FROM TABLE where date filed = todays date, I want it to only add an AND clause if the text input has a none null value.

So it becomes SELECT * FROM TABLE WHERE datefield = todays date AND field_name like (textInput1.value)

In pseudo code I want to so be

SELECT * FROM TABLE WHERE date_field = todays date
(IF input value is not null) {
AND name_field = input value
}

How do I do that from inside the query?

Thanks.

Hi Chris,

you can do that by making it part of your sql query's logic with something like this:

SELECT * FROM table
WHERE 
      datefield = todays date AND 
      (
          {{textInput1.value === ""}} OR
          field_name like {{textInput1.value}}
    )

If the field is empty, it'll basically be this query

SELECT * FROM table
WHERE 
      datefield = todays date AND 
      (
          true OR
          field_name like ''
    )