Use IF function in SQL as if it were excel [closed]

1

Is there a function in SQL equivalent to IF ( SE ) of excel? I need to do a logical test that in excel I would write =SE(B1_UM = "UN"; D2_QUANT / B5_QE1;D2_QUANT) .

Here is my query:

SELECT
   D2_FILIAL,
   // Um monte de coluna...
   CASE
      B1_UM 
      WHEN
         'UN' 
      THEN
         D2_QUANT / B5_QE1 
      ELSE
         D2_QUANT 
   END AS CALC_QUANT 
FROM
   SD2010 SD2 
   // Vários joins
WHERE com várias condições
GROUP BY
   D2_FILIAL, 
   // Várias outras colunas no group
   CALC_QUANT
    
asked by anonymous 16.05.2018 / 20:43

2 answers

0

To handle conditional results in > You basically have three structures:

1. IIF ( Boolean expression , value if true , value if false )

Example:

SELECT IIF(id = 1, 'VERDADEIRO', 'FALSO') as Expressao
FROM tabela

2. CASE expression WHEN 1 THEN result 1 END

Example:

SELECT CASE id 
           WHEN 1 THEN 'ID é 1'
           WHEN 2 THEN 'ID é 2'
           ELSE 'ID é outra coisa'
       END as Expressao
FROM tabela

3. CASE WHEN Boolean expression THEN result (...) END

Example:

SELECT CASE  
           WHEN id = 1 THEN 'ID é 1'
           WHEN id > 5 THEN 'ID é maior que cinco'
           ELSE 'Não sei o que fazer com o ID'
       END as Expressao
FROM tabela

I hope this has cleared up your questions.

    
16.05.2018 / 21:01
-1

What do you look for, I believe this to be

select iif(sexo = 0, 'Feminino', 'masculino') from clientes

Edited

Fixed the above command

As mentioned in this link , the IIF command (condition, respPositive, respNegative) should work if your SQL SERVER is 2012 +

    
16.05.2018 / 20:48