title: Concatenate calculated fields in SQL
body: <p>I have 3 columns that are string columns calculated using complex substring and replace functions.</p>
  
  <p>I have a 4th column which should be col1 + col2 + col3.</p>
  
  <p>At the moment the 4th column is repeating the calculations required to computed col1 and col2 and col3.</p>
  
  <p>The select statement looks something like this (I've removed most of the functions):</p>
  
  <pre><code>select 
  STR(c1, 2, 0) as col1,
  STR(c2, 2, 0) as col2,
  STR(c3, 2, 0) as col3,
  STR(c1, 2, 0) + STR(c2, 2, 0) + STR(c3, 2, 0) as col4
  from blah
  </code></pre>
  
  <p>The issue is that we're repeating the functions to calculate the columns, breaking the DRY principal and opening an opportunity for errors to creep in.</p>
  
  <p>I'd prefer if we could do something like this:</p>
  
  <pre><code>select 
  STR(c1, 2, 0) as col1,
  STR(c2, 2, 0) as col2,
  STR(c3, 2, 0) as col3,
  col1 + col2 + col3 as col4
  from blah
  </code></pre>
  
  <p>Is there a neat way of doing this? I think it could be done using a temp table but that seems like overkill.</p>
  
  <p>Is it worth doing, or am I just taking DRY to seriously.</p>