title: Concatenate varchar(max) variables SQL Server 2005
body: <p>Just ran into a major headache when concatenating several <code>@varchar(max)</code> variables together to build an email based on several different queries. </p>
  
  <p>For efficiencies sake, I was using several <code>varchars</code> to build the email at once, rather than going through roughly the same query two or three or more times to build it using only one <code>varchar</code>.</p>
  
  <p>This worked, right up until my <code>varchars</code> got to longer than 8000 characters. Then the concatenation of them all into one <code>varchar</code> (which I could shove into the <code>@body</code> parameter of <code>msdb.dbo.sp_send_dbmail</code>) returned "", and even LEN() wouldn't actually give me a length.</p>
  
  <p>Anyhow, I've gotten around this by doing roughly the same queries several times and building the email with only one <code>varchar(max)</code>. </p>
  
  <p><strong>TL;DR</strong></p>
  
  <p>I'm not happy with the solution. How could I have appended these <code>varchar(max)</code> variables to each other?</p>