Hi @Johnathan Simpson ,
You could use isnull function to show 0 Instead of Null mentioned by Viorel.
In addition, you could also consider to use COALESCE which is an alternative approach of ISNULL.
Please refer to below:
alter procedure dbo.GetTagVeraCoreWOrkbook
As
DECLARE @cols AS NVARCHAR(MAX)='';
DECLARE @cols1 AS NVARCHAR(MAX)='';
DECLARE @query AS NVARCHAR(MAX)='';
DECLARE @SQL_QUERY NVARCHAR(max)
SELECT @cols = @cols + QUOTENAME(vendorsku) + ',' FROM (select distinct vendorsku from ProdInfo ) as tmp
select @cols = substring(@cols, 0, len(@cols)) --trim "," at end
SELECT @cols1 = @cols1 + ' COALESCE('+QUOTENAME(vendorsku)+',0) ' +QUOTENAME(vendorsku)+ ',' FROM (select distinct vendorsku from ProdInfo ) as tmp
select @cols1 = substring(@cols1, 0, len(@cols1))
SET @SQL_QUERY =
N'SELECT ordernumber,'+@cols1+' from
(
select od.ordernumber, psi.vendorsku, od.quantity
from orderdata od
inner join prodinfo psi
on od.productcode = psi.sku
) src
pivot
(
max(quantity) for vendorsku in (' + @cols + ')
) piv'
EXECUTE sp_executesql @SQL_QUERY
Please refer to below link for more details about the difference.
Deciding between COALESCE and ISNULL in SQL Server
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.