So I have this code below that uses a stored procedure to send an html formatted email. I got it from here:
https://stackoverflow.com/a/29708178/1452574
Now, the code runs great and I have a nice html table, but the problem is that my ROUND is not holding up. If I run the code in SSMS, it clearly works, rounds to 2 decimal places, but when I get the email, the number column looks like this:
I mean, I guess it's rounding, but I don't want to see all those extra zeroes or that plus sign at the end. I've tried casting and formatting to a million different data types, I can't seem to get a nice number with 2 decimal places. I either get a whole number (if I cast to say Numeric), or the mess you see in the above screenshot. I tried using FORMAT to N2, but got this error:
Conversion failed when converting the nvarchar value '3.39' to data type int.
Code below, it's not the whole thing, but the first part is just testing for a rowcount, so I don't think it's relevant:
EXEC spQueryToHtmlTable @html = @html OUTPUT,
@query =
N'SELECT
e.EmplName,
CASE
WHEN SUM(t.ManHrs) IS NULL THEN 0
ELSE ROUND(SUM(t.ManHrs), 2)
END AS [Hrs Logged]
FROM EmplCode e
LEFT JOIN TimeTicketDet t ON e.EmplCode = t.EmplCode
AND CAST(t.TicketDate AS DATE) = CAST(GETDATE() AS DATE)
AND t.WorkCntr <> 50
WHERE e.DeptNum LIKE ''PROD %''
AND e.Active = ''Y''
GROUP BY e.EmplName
HAVING ROUND(SUM(t.ManHrs), 2) < 6
OR ROUND(SUM(t.ManHrs), 2) IS NULL';
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Email System',
@recipients = 'sample@gmail.com',
@body = @html,
@body_format = 'HTML',
@query_no_truncate = 1,
@attach_query_result_as_file = 0,
@execute_query_database = 'CompanyDB';