![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I am using a pivot (dynamically) and am getting NULL results for some values. I want to change the NULLs to 0, but keep getting errors. In the following: @strList contains the pivot columns. This one works fine except for the nulls: SET @sql = ' select * from #tfs PIVOT ( SUM(Number) for retailer in('+@strList+') ) as pvt ' I tried: SET @sql = ' select * from #tfs PIVOT ( SUM(Number) for ISNULL(retailer,0) in('+@strList+') ) as pvt ' This gets me "incorrect syntax errors" I also tried SET @sql = ' select * from #tfs PIVOT ( ISNULL(SUM(Number),0) retailer in('+@strList+') ) as pvt ' This one gets 'isnull' is not a recognized aggregate function. Thanks, Tom |
#3
| |||
| |||
|
|
@sql comes up with the following: select * from #tfs PIVOT ( SUM(Number) for retailer in([Sears], [CompUSA], [Lowes], [None], [K-Mart]) ) as pvt |
#4
| |||
| |||
|
|
On 2010-10-26 21:37, tshad wrote: @sql comes up with the following: select * from #tfs PIVOT ( SUM(Number) for retailer in([Sears], [CompUSA], [Lowes], [None], [K-Mart]) ) as pvt I would try something like: select * from #tfs PIVOT ( SUM(coalesce(Number, 0)) for retailer in([Sears], [CompUSA], [Lowes], [None], [K-Mart]) ) as pvt |
|
Not sure whether coalesce is coalesce or something else in sql-server though. The function should return the leftmost argument that is not null, in this case if Number is null -> 0. Since I don't know the pivot function, there is a possibility that the null you see origins from it, and if that is the case the solution above wont work. |
|
/Lennart |
#5
| |||
| |||
|
|
I am using a pivot (dynamically) and am getting NULL results for some values. I want to change the NULLs to 0, but keep getting errors. In the following: @strList contains the pivot columns. This one works fine except for the nulls: |
#6
| |||
| |||
|
|
@sql comes up with the following: select * from #tfs PIVOT ( SUM(Number) for retailer in([Sears], [CompUSA], [Lowes], [None], [K-Mart]) ) as pvt Thanks, Tom |
![]() |
| Thread Tools | |
| Display Modes | |
| |