title: SQL substring charindex
body: <p>I have a SQL field with a string like this</p>
  
  <pre>
  27/08/2018 to 31/08/2018 - GCH/10561/<b>201312361J</b>/HO
  04/09/2018 to 07/09/2018 - GCH/2836/202788279H/WAL
  </pre>
  
  <p>This string has a reference code I need to extract. The one bolded above.</p>
  
  <p>The problem is the string before it has not always the same length. See second example above. </p>
  
  <p>How can I extract that code each time? </p>
  
  <p>Ive tried this, but i need to the left of the last "/".....</p>
  
  <pre><code>SELECT Right('27/08/2018 to 31/08/2018 - GCH/10561/201312361J/HO', CHARINDEX('/','27/08/2018 to 31/08/2018 - GCH/10561/201312361J/HO')-1)
  </code></pre>
  
  <p>I think I need to use a <code>substring</code> and <code>charindex</code> but cannot get it working. </p>
  
  <p>Thanks in advance for your help. </p>