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粉310754094
2023-08-25 13:26:01
<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>
Use
case
Expression:You can use nested
NULLIF()
functions to achieve:See Demo.