How not to bring a certain column in SQL, using IF and ELSE?

2

Gentlemen, I have a table where I want to bring the column when the value is null but when it is filled it should not be displayed, how can I implement this condition in SQL?

SELECT 
    OBS.NUOBS,
    OBS.DTOBS,
    OBS.CODUSU,
    OBS.OBS,
    OBS.PENDENTELOG,
    OBS.DTHSOBS,
    OBS.REGINC,
    OBS.DHPROXCONTATO,
    OBS.ID,
    OBS.FIMATT

FROM AD_PRONTUARIOOBS OBS

LEFT JOIN AD_PRONTUARIOATT ATT (NOLOCK) ON ATT.PEDIDOEXTERNO = OBS.PEDIDOEXTERNO

Now I'm developing the above query, column B would refer to table AD_PRONTUARIOATT

    
asked by anonymous 19.09.2018 / 15:46

1 answer

5

Using CASE :

SELECT 
(CASE WHEN A.campo1 IS NULL THEN B.campo1 ELSE A.campo1 END) as Resultado
FROM tabelaA A
LEFT JOIN tabelaB B ON B.id = A.idB

You can also use IIF , as said by @ RobertodeCampos

SELECT 
IIF(A.campo1 IS NULL, B.campo1, A.campo1) as Resultado
FROM tabelaA A
LEFT JOIN tabelaB B ON B.id = A.idB
    
19.09.2018 / 15:48