title: Reducing redundancy/duplication in SQL Code
body: <p>In <a href="https://stackoverflow.com/questions/5578379/substring-domainname-from-url-sql">this question</a> on stackoverflow, the accepted answer involves repeating the same code snippets several times.  In my experience, many people strive to reduce that by encapsulating the code snippets in various ways, for various reasons;<br>
  - Maintainability (fewer places to change)<br>
  - Readability     (read the code once, then it's 'alias' each subsequent time)<br>
  - etc, etc</p>
  
  <p>Using the code in the linked question, how would you set out to reduce the repetition of identical code snippets?  Or would you just leave it as-is?</p>
  
  <p>(Not using alternative logic, but sticking to the use of REPLACE, etc, just changing the presentaiton of the same logic.)</p>
  
  <pre><code>Select  Case
          When CharIndex('.', Replace(URL, 'www.','')) &gt; 0 then
             Left(Replace(URL, 'www.',''), CharIndex('.',Replace(URL, 'www.',''))-1)
          Else
             Replace(URL, 'www.','')
          End as [CleanURL]
  From dbo.YourTable 
  </code></pre>
  
  <p>(The accepted answer changed, so I copied the code example to here.)</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Just to clarify, as I think I have caused confusion.</p>
  
  <p>This is not:<br>
  - How do I encapsulate this entire piece of code for re-use?  </p>
  
  <p>But it is rather:<br>
  - How do I reduce rendundancy within this piece of code?  </p>