Functions to filter missing values in SQL and change them to null values

28 views Asked by At

Which SQL query string or function could I use to filter "zero" values from a dataset and change them to "null" values?

I tried to run a query to filter the "zero" values in a dataset and change them to "null" values, but I couldn't.

1

There are 1 answers

0
Schwern On

You can use nullif which returns null if both values match, otherwise it returns the first value.

select
  some_int,
  nullif(some_int, 0),
  some_text,
  nullif(some_text, '0')
from some_table

nullif is specific to your problem. More generically you'd use a case.

select
  some_int,
  case some_int
    when 0 then null
    else some_int
  end as "case",
  some_text,
  case some_text
    when '0' then null
    else some_text
  end as "case"
from some_table;

Demonstration.