List: General Discussion « Previous MessageNext Message » From: ViSolve DB Team Date: December 6 2006 10:56am Subject: Re: SUM() of 1 and NULL is 1 ? View as plain text
```Hi Vegelin,

This will do..

select IF(SUM(IF(Jan IS NULL, 0, Jan))>0,NULL,SUM(IF(Jan IS NULL, 0,
Jan)))as jan from data;

Thanks,
ViSolve DB Team

----- Original Message -----
From: "ViSolve DB Team" <mysql_support@stripped>
To: "C.R.Vegelin" <cr.vegelin@stripped>; <mysql@stripped>
Cc: <mysql_support@stripped>
Sent: Wednesday, December 06, 2006 4:18 PM
Subject: Re: SUM() of 1 and NULL is 1 ?

> Hi Vegelin,
>
> In MySQL,
>
> SUM(1) = 1
> SUM(NULL) = NULL
>
> In your case, you are inserting two values 1, NULL to Jan column, So now
> Jan column contains 1 and NULL, If you SUM the Jan then the result will be
> 1 not NULL. Because SUM() ignores NULL values.
>
> As far as i know, we dont have built-in function that can be used in SUM()
> to convert values into NULL. We have lot of function to covert NULL into
> values. To get a NULL value instead on 1, you have to write a stored
> procedure or function with the logic given below.
>
> If Column jan contains NULL then return NULL
> ELSE return SUM(jan)
>
> Thanks,
> ViSolve DB Team
>
>
>
> ----- Original Message -----
> From: "C.R.Vegelin" <cr.vegelin@stripped>
> To: <mysql@stripped>
> Sent: Wednesday, December 06, 2006 3:27 PM
> Subject: SUM() of 1 and NULL is 1 ?
>
>
> Hi List,
>
> I need to SUM() on months from a table like:
> CREATE TABLE `data`
> ( `Jan` float default NULL,
> ...
> ) ENGINE=MyISAM; # V 5.0.15
>
> Months may have NULL values, like:
> INSERT INTO data (Jan) VALUES (1), (NULL);
>
> However, when I use SELECT SUM(Jan) AS Jan,
> the returned value is 1 in stead of NULL.
> How to get a result NULL when in such a case ?
>
> TIA, Cor
>
>
> --------------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.409 / Virus Database: 268.15.9/573 - Release Date: 12/5/2006
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:
> http://lists.mysql.com/mysql?unsub=1
>
>
>
> --
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.409 / Virus Database: 268.15.9/573 - Release Date: 12/5/2006
>
>

```