title: SQL Server : index of multiple characters
body: <p>I recently found out that my links stored in the database do not open in new tabs (no <code>target="_blank"</code>). I made this script that adds it in. </p>
  
  <p>My problem is that if the <code>Preamble</code> column has multiple links, only the first one will be updated with <code>target="_blank"</code>, how can I run this for all links in the column?</p>
  
  <pre><code>SELECT 
      STUFF(Preamble, CHARINDEX('&gt;', Preamble, PATINDEX('%&lt;a href%', Preamble)) - 1, 1, '" target="_blank"')
  FROM 
      QuestionContainer
  WHERE 
      Preamble LIKE '%&lt;a href%'
  </code></pre>
  
  <p>The Preamble column contains other HTML markup and other text.</p>
  
  <p><strong>Update</strong></p>
  
  <p>So for some reason I was trying to add the <code>target="_blank"</code> to the end of the anchor tag, instead I can just add it to the start.<br>
  A simple <code>REPLACE(Preamble, '%&lt;a href%', '&lt;a target="_blank" href')</code> will hit all occurrences in a row and solve my problem. - Thanks Allan S. Hansen</p>