Insert if in the selected query

How can I do something like this?

SELECT (IF(SUM(Costo) > 0) SUM(Costo) ELSE 0) AS Expr1 FROM TDP_NotaSpeseSezB 
+4
source share
3 answers

You want to use the CASE statement.

 Select CASE WHEN SUM(Costo) > 0 THEN SUM(Costo) ELSE 0 END 'Expr1' FROM TDP_NotaSpeseSezB 
+4
source

You can use case register as follows:

 SELECT case when sum(Costo)> 0 then sum(Costo) else 0 end as Expr1 FROM TDP_NotaSpeseSezB 

CASE (Transact-SQL)

+1
source
 SELECT CASE WHEN SUM(Costo) > 0 THEN SUM(Costo) ELSE 0 END AS Expr1 FROM TDP_NotaSpeseSezB 

Other core engines support this syntax:

 SELECT GREATEST(SUM(Costo), 0) FROM TDP_NotaSpeseSezB 

SQL Server , however, does not.

0
source

All Articles