riki
riki

Reputation: 1725

Condition on SQL Server query

The below SQL query is in a SQL Server 2017. I want when the value of UtenteCliente.CostoFatturazionePersonalizzato is 0 or null is configured with the value of Utente.CostoFatturazione, to do this I use the query below but the result always returns null.

How can I do this?

SQL Query:

SELECT 
    IIF (UtenteCliente.CostoFatturazionePersonalizzato < 0
         OR UtenteCliente.CostoFatturazionePersonalizzato IS NULL, 
         Utente.CostoFatturazione, UtenteCliente.CostoFatturazionePersonalizzato) AS costo
FROM 
    Utente
INNER JOIN 
    UtenteCliente ON Utente.IdUtente = UtenteCliente.IdUtente
WHERE 
    Utente.IdUtente = 2
    AND UtenteCliente.IdCliente = 6

Upvotes: 0

Views: 62

Answers (1)

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

You can use CASE expression instead IIF():

SELECT (CASE WHEN ISNULL(UtenteCliente.CostoFatturazionePersonalizzato, 0) = 0
             THEN Utente.CostoFatturazione
             ELSE UtenteCliente.CostoFatturazionePersonalizzato
        END) AS costo
FROM Utente INNER JOIN 
     UtenteCliente 
     ON Utente.IdUtente = UtenteCliente.IdUtente
WHERE Utente.IdUtente = 2 AND 
      UtenteCliente.IdCliente = 6;

Upvotes: 1

Related Questions