title: How do I use SUBSTRING and CHARINDEX in SQL to return only the right part of a field contained within brackets if multiple brackets are in the field?
body: <p>I have a column of data that contains varchar data and I've been trying to return only the section contained with brackets using CHARINDEX and SUBSTRING :</p>
  
  <p>column data: <strong>'this is an (example)'</strong> </p>
  
  <p>I use the following code to return 'example' from the above data:</p>
  
  <pre><code>SELECT SUBSTRING (column,  CHARINDEX('(', column)+1 ,   CHARINDEX(')', column)- CHARINDEX('(', column)-1 )
  </code></pre>
  
  <p>This works fine, however, in some instances, the data in the field has multiple occurrences of data between two brackets: </p>
  
  <p><strong>- 'this (is) an (example)'</strong></p>
  
  <p>This means that my code above returns 'is'.  In my data, what I want to return is the data in the right most set of brackets:</p>
  
  <ul>
  <li><strong>'this (is) an (example)'</strong> - I would want to return <strong>'example'</strong></li>
  <li><strong>'this (is) some (text)'</strong> - I would want to return <strong>'text'</strong></li>
  <li><strong>'this (is) definitely (not) a (number)'</strong> - I would want to return <strong>'number'</strong></li>
  </ul>
  
  <p>etc</p>