使用pivot转换空值

时间:2017-02-17 14:57:08

标签: sql-server

我试图在我的脚本中转换NULL值我也在使用PIVOT函数。这是我的脚本,我尝试将NULL转换为" 0"

声明@custnum nvarchar(7)=' 1124' 声明@yr smallint = 2016

SELECT 'Customer Orders Returned' as Description, *
FROM (
SELECT left(datename(month,r.rma_date),3) AS RetMonth, 
ISNULL(COUNT(r.rma_num),0) as OrdersReturned
FROM dbo.rma r
WHERE r.cust_num = @custnum AND YEAR(r.rma_date) = @yr
GROUP BY left(datename(month,r.rma_date),3),r.rma_num
)
AS R

PIVOT
(
SUM(OrdersReturned)
FOR RetMonth
IN (Jan,Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
)AS pvt

我的结果:

Results Table

非常感谢任何有助于解决我的问题的建议。

谢谢!

1 个答案:

答案 0 :(得分:0)

使用ISNULL()函数

Select ISNULL(columnname,0) from tablename