Select a column if other column is null

后端 未结 7 735
庸人自扰
庸人自扰 2020-12-08 13:14

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

7条回答
  •  隐瞒了意图╮
    2020-12-08 13:29

    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'
    

提交回复
热议问题