sp_send_dbmail issue when using variable for subject line When using a variable for the subject line for a database trigger email, I get the variables back in the subject line but they look like this:

&lt;i loan_id="6686"&gt;&lt;cm cust_name="John Bonham  IIIII"&gt;&lt;ld loan_number="5555"&gt;&lt;e FAMILIAR_NAME="Test User"/&gt;&lt;/ld&gt;&lt;/cm&gt;&lt;/i&gt;

Is there a way to clean it up so that it looks better? For instance remove the &lt;>?

   SET @subject = 
    CAST ( ( SELECT top 1 i.loan_id,cm.cust_name,  ld.loan_number,  e.FAMILIAR_NAME
              FROM inserted i inner join deleted d on i.LOAN_ID = d.Loan_ID and i.STATUS = 4
                inner join Status s  on i.STATUS = s.Status_ID 
                inner join Loan_Details ld on i.LOAN_ID = ld.loan_id and ld.LOAN_ID = d.LOAN_ID
                and ld.STATUS = s.Status_ID
                inner join Employee e on ld.OFFICER = e.EMP_ID
                inner join REL_CUST rc on rc.LOAN_ID = ld.loan_id
                inner join Customer_Master cm on cm.CUST_ID = rc.CUST_ID and cm.CUST_ID = rc.REL_TRAN_ID 
                and relationship_id = 1

          WHERE i.STATUS &lt;&gt; d.STATUS and i.STATUS = 4 and relationship_id = 1 and d.STATUS &lt;&gt;4
          ORDER BY i.loan_id ASC
          FOR XML PATH('') 
) AS NVARCHAR(MAX) ) ;
An option would be to enhance the FOR XML PATH('') to pull the actual node values out.  

See the following examples (with the results of both at the bottom).

(Original represents what you are basically doing now)

(Updated uses the enhanced FOR XML PATH('') to pull the actual node values out).  

set nocount on
Declare @T Table (LoanId int, FirstName varchar(100), LastName varchar(100))
insert into @T(LoanId,FirstName,LastName) values 
    (12345,'John','Smith'),(54321,'James','Smith')

declare @subject varchar(100)

--Original statement
SET @subject = cast((
            SELECT TOP 1 LoanId, FirstName, LastName
            FROM @t
            FOR XML PATH('')) AS NVARCHAR(MAX) ) ;

print @subject

--Updated statement
SET @subject = (
            SELECT TOP 1 'LoanId = ' + convert(varchar(10),Loanid) + ',' + 'FirstName = ' + FirstName + ',' + 'Lastname = ' + lastname
            FROM @t
            FOR XML PATH(''),TYPE).value('.','NVARCHAR(MAX)');

print @subject




&lt;LoanId&gt;12345&lt;/LoanId&gt;&lt;FirstName&gt;John&lt;/FirstName&gt;&lt;LastName&gt;Smith&lt;/LastName&gt;
LoanId = 12345,FirstName = John,Lastname = Smith