Select a column if other column is null

107,116

Solution 1

You need the ISNULL function.

SELECT ISNULL(a, b)

b gets selected if a is null.

Also, you can use the WHEN/THEN select option, lookup in BOL. Essentially: its c switch/case block meets SQL.

Solution 2

You can use either the ISNULL function or the COALESCE function. They both do pretty much the same thing, however ISNULL only takes two parameters and COALESCE takes multiple parameters (returning the first non-null it encounters). Both try the first param, then the second, (and COALESCE continues on)

DECLARE @IAMNULL VARCHAR
DECLARE @IAMNOTNULL VARCHAR
SET @IAMNOTNULL = 'NOT NULL'

SELECT ISNULL(@IAMNULL, @IAMNOTNULL)
--Output: 'NOT NULL'

DECLARE @IAMNULLALSO VARCHAR

SELECT COALESCE(@IAMNULL, @IAMNULLALSO, @IAMNOTNULL)
--Output: 'NOT NULL'

Solution 3

  select COALESCE ( ProgramID , InterimProgramID ) as 'ProgramID' 

Solution 4

There is also:

Select NVL(Column_A, Column_B) From 'schema'.'table_name'

The NVL( ) function is available in Oracle, and not in MySQL or SQL Server. This function is used to replace NULL value with another value. It is similar to the IFNULL Function in MySQL and the ISNULL Function in SQL Server.

https://www.1keydata.com/sql/sql-nvl.html

Solution 5

SELECT ProgramID
  FROM a_table
 WHERE ProgramID IS NOT NULL
UNION
SELECT InterimProgramID AS ProgramID
  FROM a_table
 WHERE ProgramID IS NULL;
Share:
107,116
Ronald McDonald
Author by

Ronald McDonald

I have really big feet and only have one pair of shoes. Edit May 2021. I woke up in Aug of 2017 and just couldn't make myself go into a job I hated so I called in sick that day and thought about it all day. The next day I emailed my boss and told her I quit effective immediately. I had already been dabbling in real estate investing and just decided to attempt it full time. It worked, I haven't gone back to working in IT or for anyone one else. I own my time and it was the best decision I ever made. Just wanted to explain why I'm not posting on here anymore. Thank you to everyone who answered my questions and wasn't an A-hole about it.

Updated on July 05, 2022

Comments

  • Ronald McDonald
    Ronald McDonald about 2 years

    I need to select a field called ProgramID from a table and if the ProgramID is NULL then I need to select the value in the InterimProgramID from the same table and alias it as ProgramID.

    How can I make a conditional SELECT statement to do this?

  • dominicbri7
    dominicbri7 over 8 years
    This question is about SQL Server and IFNULL does not exist with that DBMS, only ISNULL
  • giorgio79
    giorgio79 almost 7 years
    MySQL equivalent is IFNULL dev.mysql.com/doc/refman/5.7/en/…
  • Peter Aron Zentai
    Peter Aron Zentai almost 7 years
    and its a || b in javascript
  • 1_bug
    1_bug about 3 years
    PostgreSQL equivalent is NULLIF postgresql.org/docs/current/…
  • Peter Aron Zentai
    Peter Aron Zentai about 3 years
    I mean the question is strictly tagged for tsql
  • Vojtech Stas
    Vojtech Stas almost 3 years
    This solution seems to answer the question best
  • Akaisteph7
    Akaisteph7 about 2 years
    @PeterAronZentai That doesn't mean people won't find this if looking for this same problem for other SQL variants. And Postgres equivalent is actually COALESCE