You can use Isnull keyword, You can decide what to decide if null value comes.
isnull(CONVERT(nvarchar(max),Month(@month)),'')
In the above code, i just pass empty string('') if its null.
am
declare @userid int=null
declare @complaintstatusid int=null
declare @fromdate date=null
declare @todate date =null
declare @month date=null
DECLARE @PivotColumnHeaders VARCHAR(MAX)
SELECT @PivotColumnHeaders =
COALESCE(
@PivotColumnHeaders + ',[' + cast(Name as varchar) + ']',
'[' + cast(Name as varchar)+ ']'
)
FROM employee A
DECLARE @PivotTableSQL NVARCHAR(MAX)
select @PivotTableSQL = 'select * from
(
select S.SchoolName, A.AssetType, S.SchoolId,um.Username +'' ''+um.Surname as Name,csm.ComplaintStatus
from [MH.Complaints] C
inner join [Mh.AssetType_Master] A
on A.AssetTypeId = C.AssetTypeID
inner join [Mh.school_Master] S
on S.SchoolId = C.SchoolID
inner join [MH.UserSchoolAssociation] usa
on S.SchoolId=usa.SchoolId
inner join [MH.User_Master] um
on usa.UserId=um.LoginId
inner join [MH.ComplaintStatus_Master] csm
on C.ComplaintStatusId=csm.ComplaintStatusId
where Month(c.Reporteddate)= coalesce('+isnull(CONVERT(nvarchar(max),Month(@month)),'null')+',Month(c.Reporteddate))
) PivotData
Pivot
(
COUNT(SchoolId)
FOR AssetType IN (
' + @PivotColumnHeaders + '
)
) AS PivotTable
'
select @PivotTableSQL