Using CSS to control formatting in dbmail

Sorry for the delayed response but I found the solution. When I converted the td to money, it right aligned the columns. It also did this when I concatenated the $ and added the "," inside the 5000.

In the latter part of the query which is not written above, I did have to do some tweaking to the query. It has both varchar and numeric columns in the td. I had to redefine the classes of my table. See below:

DECLARE @bodyMsg nvarchar(max)
DECLARE @subject nvarchar(max)
DECLARE @Style nvarchar(max)= '';
DECLARE @tableHTML nvarchar(max)= '';

SET @subject = 'Matter Alert for 101853'

SET @Style +=

  • N'' +
    N'.tg
    {
    font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
    font-size: 12px;
    text-align: left;
    border-collapse: collapse;
    border-top: 7px solid #9baff1;
    border-bottom: 7px solid #9baff1;
    }
    ' + N'.tg td
    {
    padding: 5px;
    border-right: 1px solid #aabcfe;
    border-left: 1px solid #aabcfe;
    border-bottom: 1px solid #aabcfe;
    color: #669;
    }
    ' + N'.tg th
    {
    padding: 5px;
    font-size: 13px;
    font-weight: normal;
    background: #b9c9fe;
    border-right: 2px solid #9baff1;
    border-left: 2px solid #9baff1;
    border-bottom: 2px solid #9baff1;
    color: #039;
    }

' + N' tr:nth-child(odd) { background-color:#eee; }
' + N' tr:nth-child(even) { background-color:#fff; }
' + N'
' + N''
;

SET @tableHTML += @Style + @tableHTML +
N'

' --DEFINE TABLE
/*
Define Column Headers and Column Span for each Header Column
*/

  • N'
'

/*
Define each th
*/

  • N'
'
  • N'
  • '
  • /*
    Define data for table and cast to xml
    */

    CAST ( (

    SELECT
    'left' as 'td/@align', td = itdr.[Employee Name],'',
    'right' as 'td/@align', td = '$' + PARSENAME(CONVERT(VARCHAR,CAST(cast(sum(itdr.[Base Amt]) as numeric(25,2)) AS MONEY),1),2)
    FROM Intl_Tax_Data_Run itdr
    where itdr.[Matter Code] = '189274'
    group by itdr.[Employee Name]FOR XML PATH('tr'), TYPE
    ) AS NVARCHAR(MAX) ) +
    N'

    Employee Name Base Rate Value
    '

    declare @nbody varchar(max)

    set @nbody = @tableHTML

    EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'ProfileName',
    @recipients='s_smith@sample.com',
    @from_address = 's_smith@sample.com',
    @subject = @subject,
    @body = @nbody,
    @body_format = 'HTML' ;