title: Parse a string before the Last Index Of a character in SQL Server
body: <p>I started with this but is it the best way to perform the task? </p>
  
  <pre><code>select  
      reverse(
          substring(reverse(some_field),
          charindex('-', reverse(some_field)) + 1, 
          len(some_field) - charindex('-', reverse(some_field)))) 
  from SomeTable
  </code></pre>
  
  <ol>
  <li>How does SQL Server treat the
  multiple calls to
  <code>reverse(some_field)</code>?</li>
  <li>Besides a UDF and iterating through
  the string looking for <code>charindex</code>
  of the '-' and storing the last
  index of it, is there a more
  efficient way to perform this task in T-SQL?</li>
  </ol>
  
  <p>Note that what I have works, I just am really wondering if it is the best way about it.</p>
  
  <p>Below are some sample values for some_field.</p>
  
  <pre><code>s2-st, s1-st, s3-st, s3-sss-zzz, s4-sss-zzzz
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Sample output for this would be...</p>
  
  <pre><code>s1, s2, s3-sss, s3, s4-sss
  </code></pre>
  
  <p>The solution ErikE wrote is actually getting the end of the string so everything after the last hyphen.  I just modified his version to get everything before it instead using a similar method with the <code>left</code> function.  Thanks for all of your your help.  </p>
  
  <pre><code>select left(some_field, abs(charindex('-', reverse(some_field)) - len(some_field)))
      from (select 's2-st' as some_field
          union select 's1-st'
          union select 's3-st'
          union select 's3-sss-zzz'
          union select 's4-sss-zzzz') as SomeTable
  </code></pre>