I am trying to return all Col1 values IF the respective Col2 value = NULL I have thought of IF Col2 = NULL Return Col2 END IF, But I am unsure of how to format this, because when I run it I get errors saying invalid relational operator. Thank you for your time.
SELECT Col1 FROM mytable WHERE IF NULL Col2 = 0;
IF Col2 = NULL RETURN COL1 ELSE END IF;
================================================================ EDIT
I apologize for the lack of information/Show of research. First post, So I have been searching google for different IF then statements to deal with NULL values. Everything I have found this far has only showed how to Ignore/convert to 0 so that you can easily work with them. Nothing on how to use them to return values from another col1
Advertisement
Answer
SELECT Col1 FROM mytable WHERE Col2 IS NULL
No IF
is required. WHERE
is the SQL IF
for row selection. This returns only the rows where Col2
is null
.
If you want to return all the rows, but want to return Col1
when Col2
is null
and NULL
otherwise, use
SELECT CASE WHEN Col2 IS NULL THEN Col1 ELSE NULL END as myResultColumn FROM mytable