![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
Using coalesce is the same sort of solution as using isnull. It doesn't behave as my requirements state. In particular, the result will be 0 if all inputs are null. It is required that the result be null. Thanks anyway. |
#4
| |||
| |||
|
|
On May 29, 4:37 pm, i... (AT) vanoordt (DOT) nl wrote: Using coalesce is the same sort of solution as using isnull. It doesn't behave as my requirements state. In particular, the result will be 0 if all inputs are null. It is required that the result be null. Thanks anyway. No. Did you test Result will be null if all are null . since COALESCE(Col1, Col2, Col3) returns null and 0 + 0 + 0 + null is null COALESCE takes more arguments and ISNULL only two declare @a table (col1 int,col2 int,col3 int) insert into @a values (1,null,null) insert into @a values (null,2,null) insert into @a values (null,null,3) insert into @a values (1,2,null) insert into @a values (null,2,3) insert into @a values (1,null,3) insert into @a values (null,null,null) select COALESCE(Col1, 0) + COALESCE(Col2, 0) + COALESCE(Col3, 0) + COALESCE(Col1, Col2, Col3) from @a 2 4 6 4 7 5 NULL |
#5
| |||
| |||
|
#6
| |||
| |||
|
|
I was actually thinking more of a solution to ignore the nulls, rather than on the fly setting them to 0. |

#7
| |||
| |||
|
|
Hi, I need this behaviour: 1 + null = 1 I have a (dynamic) set of many columns containing decimals that I want to add as follows: if all columns are null the result should be null if not all columns are null, the null columns may be regarded as 0. E.g. null + null + 1 = 1 null + null + null = null The problem is that the first expression yields null. Up till now I generated an update statement with isnull(<column>,0), however, then the second expression yields 0. I can add another update statment setting the result to null if all columns are null, but this is very slow, and not very intuitive either. How nice it would be if there were a setting like 'concat null yields null' for arithmetic operators. Anyone any idea how to fix this? Thanks. Paul |
#8
| |||
| |||
|
|
There is this problem with Srinivas' solution and the solution Robert supplies is actually what I already proposed myself. Namely separating the case where all columns are null from those cases where some ar not null, and this is very slow. (I'm talking about hundreds of columns and millions of rows.) |
#9
| |||
| |||
|
#10
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |