Line breaks in HTML tables

Paddy Mullaney 21 Reputation points
2022-01-02T20:53:59.603+00:00

Hello I am using TSQL and an HTML table to run a query and send the results via an email. I cannot preserve the line breaks in the result set.

I have some test code below. When I run the query via management studio I get the results;

line

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Line 1

Line 2

Line 3

But the email output shows;

Line 1 Line 2 Line 3

Can I preserve the line breaks in sp_sendmail?

code below -
/////////////////////////////////////////////////////

create table test (line nvarchar(max))
go
declare @testtext nvarchar(max)
set @testtext = 'Line 1' + char (10)+ CHAR(13) + 'Line 2' + char (10)+ CHAR(13) + 'Line 3'

select @testtext

insert into test select @testtext

declare @tableHTML nvarchar(max)
SET @tableHTML =
N'<H1>TEST</H1>' +
N'<tr>
<th>test</th>'+
'</tr>'
+
CAST ( (select
td =t.line, ' '
from
test t
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;

EXEC msdb.dbo.sp_send_dbmail

@recipients = 'me@myemailaddress',  
@subject = 'test',  
  
@body_format = 'HTML' ,  

@body = @tableHTML ;  

go
select * from test
drop table test

///////////////////////////////////////////////////

Developer technologies Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2022-01-02T21:29:44.12+00:00

    If possible, place the "Line 1", "Line 2" etc. on separate rows of test table. Or try one of workaround:

    SET @tableHTML =
    N'<H1>TEST</H1>
    <tr><th>test</th></tr>' +
    replace(CAST ( (select td = line from test FOR XML PATH('tr') ) AS NVARCHAR(MAX) ), '&#x0D;', '<br/>') +
    N'</table>'
    

    (If test contains several rows, you must use some ORDER BY to send the lines in the right order).

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Ahsan Kabir 76 Reputation points MVP
    2022-01-02T21:13:49.487+00:00

    Notes 1 :Use only CHAR(13)

    declare @testtext nvarchar(max)
    set @testtext = 'Line 1' + CHAR(13) + 'Line 2' + CHAR(13) + 'Line 3'

    select @testtext

    Notes 2 : Have check setting

    161804-setting.png


  2. Paddy Mullaney 21 Reputation points
    2022-01-02T22:04:04.487+00:00

    Great thank you

    0 comments No comments

  3. Tom Phillips 17,771 Reputation points
    2022-01-04T15:27:57.067+00:00

    Just to be clear.

    HTML ignores CHAR(10) and CHAR(13) when displaying to the screen. To create HTML line breaks you must use the HTML code "<br>".

    The code Viorel posted simply replaces CHAR(10) with "<br>".

    The simpler answer it to not put in the CHAR(10)+CHAR(13) in your string at all, if you are going to use it for HTML output.

    set @testtext = 'Line 1' + '<br>' + 'Line 2' + '<br>' + 'Line 3'
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.