Macbernie Macbernie - 5 months ago 7
SQL Question

Return 0 1 instead of data values

I have a simple SELECT request

SELECT firstname, lastname, age, country
FROM mytable
ORDER BY id


returning somethink like

jean dupont 35 france
hervé yang japon
laura 28
mickael sylvain 65 suisse
...


I want returning 1 (or true) when the field is not null, and 0 (or false) when the field is null.

In my exemple, the request should return

1 1 1 1
1 1 0 1
1 0 1 0
1 1 1 1
...


Thanks for help

hvd hvd
Answer

You can select boolean expressions directly to get true or false as your result.

SELECT
  firstname IS NOT NULL,
  lastname IS NOT NULL,
  age IS NOT NULL,
  country IS NOT NULL
FROM mytable
ORDER BY id