title: Get everything before a certain character in SQL
body: <p>I got the following entries in my database:</p>
  
  <pre><code>E01234-1-1
  E01234444-1-800000000
  </code></pre>
  
  <p>I want to trim the entry so I get: </p>
  
  <pre><code>E01234-1
  E01234444-1
  </code></pre>
  
  <p>So basically, I want everything before the second '-' regardless of the length</p>
  
  <p>How can I solve it?  Im using MS SQL SERVER 2012</p>
  
  <p>I am using this but it only bring data from before first hyphen, not second hyphen</p>
  
  <pre><code>DECLARE @TABLE TABLE (STRING VARCHAR(100))
  
  INSERT INTO @TABLE (STRING)
        SELECT 'E01234-1-1' 
  UNION ALL SELECT 'E01234-1-200' 
  UNION ALL SELECT 'E01234-1-3000' 
  UNION ALL SELECT 'E01234-1-40000' 
  UNION ALL SELECT 'E01234-1-500000' 
  UNION ALL SELECT 'E01234-1-6000000' 
  UNION ALL SELECT 'E01234-1-70000000' 
  UNION ALL SELECT 'E01234444-1-800000000' 
  
  SELECT LEFT(STRING, CHARINDEX('-',STRING)-1) STRIPPED_STRING from @TABLE
  </code></pre>
  
  <p>RETURNS 
  <code>
  E01234
  E01234
  E01234
  E01234
  E01234
  E01234
  E01234
  E01234444</code></p>