Home  >  Q&A  >  body text

In SQL, how to assign the value of a column to null without using the CASE statement, using the nested NULLIF function when condition A or condition B is met

<p>I have a column C1 whose value can be 'values', 'empty' or 'N/A'. </p> <pre class="brush:php;toolbar:false;">| C1 | ------- | | | N/A | |apple|</pre> <p>I want to select column C1 in a way that converts null values ​​and N/A to NULL, using NULLIF. </p> <pre class="brush:php;toolbar:false;">| C1 | ------- |NULL | |NULL | |apple|</pre> <p>We can use <code>NULLIF(C1, '')</code>, which returns NULL if the column value is empty. </p> <p>We can also use CASE to achieve these two situations, but I want to know if there is a way to use NULLIF to achieve it, and if so, how to achieve it? (Or other methods besides CASE)</p> <p>Similar to <code>NULLIF(C1, '' OR 'N/A')</code></p> <p>Thanks in advance. </p>
P粉310754094P粉310754094422 days ago502

reply all(2)I'll reply

  • P粉253800312

    P粉2538003122023-08-26 12:34:15

    Use caseExpression:

    (case when c1 not in ('', 'N/A') then c1 end)

    reply
    0
  • P粉925239921

    P粉9252399212023-08-26 00:26:44

    You can use nested NULLIF() functions to achieve:

    NULLIF(NULLIF(c1, ''), 'N/A')

    See Demo.

    reply
    0
  • Cancelreply