How to get null value when summing a column has a null value
sql, sum, t-sql
Solution
`sum()` (as with the other aggregation functions) ignores `NULL`s when combining values from different rows. You can do what you want with additional logic:
SELECT (case when count(*) = count(oran*deger)
then SUM(oran*deger/100)
end)
FROM @tablo;
The construct `count(*) = count(<whatever>)` is the shortest way I can think of to determine if a value is `NULL`. The first part counts the number of rows in the group. The second counts the number of non-NULL values. If these are different, then there is a `NULL` value somewhere.
Problem
``` DECLARE @tablo TABLE (oran INT,deger INT) INSERT INTO @tablo SELECT 10,NULL UNION ALL SELECT 10,20 SELECT oran*deger/100 FROM @tablo SELECT SUM(oran*deger/100) FROM @tablo SELECT NULL+2 ``` When i use sum function it returns a value but if a column has a null value , i want it to return null. How is it possible? Thanks in advance...