Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 return 0 instead of null with pivot

Author  Topic 

cognos79
Posting Yak Master

241 Posts

Posted - 2010-07-29 : 12:13:14
when I do pivot on date column...if some of the dates are not in the table then pivot returns null, but I want to show 0 in the column.

exec('select pvt.*
from #final
pivot
(
sum(totCount)
for attend_date in (' + @dates +')
) as pvt')

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2010-07-29 : 12:32:36
List the columns rather than pvt.*, but I think this works, but I havn't worked with a pivot in a little while.

exec ('select pvt.col1,pvt.col2,pvt.etc, coalesce(totcount,0) as Totcount
from #final
pivot
(
sum(totCount)
for attend_date in (' + @dates +')
) as pvt'


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

cognos79
Posting Yak Master

241 Posts

Posted - 2010-07-29 : 12:36:39
the number columns in the pivot is dynamic...so I wont be able to list them.
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2010-07-29 : 13:51:52
Please show some sample data if this is not what you want.

exec('select pvt.col1,pvt.col2,pvt.etc, coalesce(totcount,0) as Totcount
from #final
where not totcount is null
pivot
(
sum(totCount)
for attend_date in (' + @dates +')
) as pvt'


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page
   

- Advertisement -