replace null values in sql pivot

2019-02-21 13:50发布

I have the following query :

SELECT *
FROM Table1
PIVOT
(
  SUM(Value)
  FOR [Period] IN ([06/1/2007],[07/1/2007])
)
AS p

Some of the rows returned are null but i want to replace them with 0.

I've tried SUM(ISNULL(Value,0)) as Val but it's not working. ( it's saying incorrect syntax)

1条回答
欢心
2楼-- · 2019-02-21 14:11

Ohh, I was using ISNULL in the wrong place.

the query should look like this:

SELECT ID,ISNULL([06/1/2007],0), ISNULL([07/1/2007],0)
FROM Table1
PIVOT
(
  SUM(Value)
  FOR [Period] IN ([06/1/2007],[07/1/2007])
)
AS p
查看更多
登录 后发表回答