Solved

MS SQL SP Result NULL skews HTML formatting

Posted on 2014-03-11
4
467 Views
Last Modified: 2014-03-12
Good morning experts!

I have a stored procedure that runs every night on my MS SQL 2005 Database.  It takes and emails the results of the query in HTML Format, but if any part of the row is NULL then even the <TD></TD> are not created...which "skews" the whole table.  It's not common, but at least one day a week a report will have at least one row that is "skewed".

I'm fine with the report sending an empty email if there was nothing done that day, but if a cell is NULL I still need the <TD></TD> elements.  I tried adding ' ' to each column results...that failed because it can't add a blank space to (nothing).

SET @tableHTML =
    N'<H1>SLX report for activities on ' + @Today + '</H1>' +
    N'<table border="1">' +
    N'<tr><th>Account</th><th>Contact Name</th><th>Opportunity Name</th>' +
    N'<th>Description</th><th>Activity Type</th><th>Notes</th><th>Username</th>' + '</tr>' +
    CAST ( ( SELECT td = ' ' + [ACCOUNTNAME],       '',
                    td = ' ' + [CONTACTNAME], '',
                    td = ' ' + [OPPORTUNITYNAME], '',
                    td = ' ' + [DESCRIPTION],	'',
                    td = ' ' + [CATEGORY],	'',
                    td = ' ' + [NOTES],	'',
                    td = ' ' + [USERNAME]
		from sysdba.History
		where CAST(CREATEDATE as Date) = convert(NVARCHAR,GetDate(), 112)
              FOR XML PATH('tr'), TYPE 
    ) AS NVARCHAR(MAX) ) +
	N'</table>' ;

Open in new window


Essentially, I need to ensure that every row contains the same number of columns regardless if a value is present or not.  Any Thoughts?
0
Comment
Question by:cjake2299
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 400 total points
ID: 39921596
Instead of [WHATEVER], use ISNULL([WHATEVER], '')
0
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 100 total points
ID: 39921860
-- essentially like Jim Horn mentioned above:

SET @tableHTML =
    N'<H1>SLX report for activities on ' + @Today + '</H1>' +
    N'<table border="1">' +
    N'<tr><th>Account</th><th>Contact Name</th><th>Opportunity Name</th>' +
    N'<th>Description</th><th>Activity Type</th><th>Notes</th><th>Username</th>' + '</tr>' +
    CAST ( ( SELECT td = ' ' + isnull([ACCOUNTNAME],''),       '',
                    td = ' ' + isnull([CONTACTNAME],''), '',
                    td = ' ' + isnull([OPPORTUNITYNAME],''), '',
                    td = ' ' + isnull([DESCRIPTION],''),      '',
                    td = ' ' + isnull([CATEGORY],''),      '',
                    td = ' ' + isnull([NOTES],''),      '',
                    td = ' ' + isnull([USERNAME],'')
            from sysdba.History
            where CAST(CREATEDATE as Date) = convert(NVARCHAR,GetDate(), 112)
              FOR XML PATH('tr'), TYPE
    ) AS NVARCHAR(MAX) ) +
      N'</table>' ;
0
 

Author Closing Comment

by:cjake2299
ID: 39924187
You ever have those days where you get so deep into a project that you forget some of the simple things?  Thanks gents, I appreciate the help!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39924353
Better you ask us and fork over these virtual things called 'points', then ask your buddies what might be a stupid question.

Thanks for the split.  Good luck with your project.  -Jim
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
connection to SQL 2012 error in windows 10 18 49
rolling count by date, hour query 7 31
SQL Query 9 29
How do i delete the last node in an xml in T-SQL 7 29
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question