DECLARE @tableHTML NVARCHAR(MAX) ;
SET @tableHTML =
N'<H1>Report Heading</H1>' +
N'<table border="1">' +
N'<th>Check Number</th>' +
N'<th>Last Operator Date</th>' +
N'<th>Last Timestamp</th>' +
N'<th>Run Date</th>' +
N'<th>Issued Check Number</th>' +
N'<th>Error Description</th>' +
'<tr>' +
CAST ( ( SELECT td = S.CHK_NUM, '',
td = S.LAST_OPER_ID, '',
td = S.LAST_TIMESTMP, '',
td = S.RUN_DT, '',
td = ISNULL(S.RE_ISSUE_CHK_NUM,-1), '',
td = ISNULL(S.ERR_DESC,'<null>'), ''
FROM STAGNG_CDDP_ERR_RCD S
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;
PRINT @tableHTML
If I attempt to substiture -1 with a varchar literal ‘(Null)’, I get the error
Msg 245, Level 16, State 1, Line 3
Conversion failed when converting the varchar value 'Null' to data type int.
I would prefer to display if the RE_ISSUE_CHK_NUM is null, but that field is an int and i believe the “TYPE” param is forcing teh returned value to match the original column’s type.
How do I modify the output so all of teh columns are text and hence formattable?
Edit
When I try this…
DECLARE @tableHTML NVARCHAR(MAX) ;
SET @tableHTML =
N'<H1>Report Heading</H1>' +
N'<table border="1">' +
N'<th>Check Number</th>' +
N'<th>Last Operator Date</th>' +
N'<th>Last Timestamp</th>' +
N'<th>Run Date</th>' +
N'<th>Issued Check Number</th>' +
N'<th>Error Description</th>' +
'<tr>' +
CAST ( ( SELECT td = S.CHK_NUM, '',
td = S.LAST_OPER_ID, '',
td = S.LAST_TIMESTMP, '',
td = S.RUN_DT, '',
--td = ISNULL(S.RE_ISSUE_CHK_NUM,-1), '',
td = CONVERT(NVARCHAR(16), ISNULL(S.RE_ISSUE_CHK_NUM,'(Null)')), '',
td = ISNULL(S.ERR_DESC,'<null>'), ''
FROM STAGNG_CDDP_ERR_RCD S
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;
PRINT @tableHTML
I get this:
Msg 245, Level 16, State 1, Line 3
Conversion failed when converting the varchar value '(Null)' to data type int.
It’s tthe Check Number field that is Null that is numeric that i would like to display the string ‘Null’ in the HTML.
Is it the TYPE param that is the issue?
change this line
to