How to nullfill variable (equivalent of SQL coalesce)

Options

I keep running into the use case of wanting to create a variable that is the first non-empty value of a series of other variables. For example for some final variable X:

  • I want X to be the first non-empty value of variables A, B, and C
  • So if A = 2, B = null, C = 1 then I want X = 2
  • If A = null, B = null, C = 1 then I want X = 1
  • If A = null, B = 3, C = 1 then I want X = 3

Is there a way to achieve that with the existing Xano variable + filter logic?

Answers

  • arturosanz
    arturosanz Member ✭✭
    Options

    As far as I know, Xano can do coalesce only in the "query all records" function.

    However, if you want to do something similar with variables, I would suggest to do the following.

    Create an array with the values of your variables. Insert them in the order you want to be parsed. Then apply the filter "filter_empty" to the array, which returns a new array with only the entries that are not empty ("", null, 0, [], {}). Then apply the filter "first" to get the first value of the new array. It should return what you are looking for.

  • mikeganz
    mikeganz Member
    Options

    Thanks @arturosanz! That's a clever idea. I was able to figure this out another way though given Xano has released conditional set filters that do basically what I'm saying. By using first_notempty I was able to mimic coalesce.