title: How to Extract in SQL string value between the 2nd set of '-' dashes in a field
body: <p>*I am trying to pull values in between 2nd set of '-' for example</p>
  
  <ul>
  <li><p><code>RNDC007-PS-160958205220402-1-A.pdf</code> should pull <code>160958205220402</code></p></li>
  <li><p><code>1883626-FA-2011978300304402-12-A.pdf</code> should pull <code>2011978300304402</code></p></li>
  </ul>
  
  <p>Below is my code I have but I am getting values with the <code>-</code> on each side of the value.</p>
  
  <p>For example: <code>-2011978300304402-</code></p>
  
  <p>Also when I run for all records against the table I get the error:</p>
  
  <blockquote>
    <p>Invalid length parameter passed to the LEFT or SUBSTRING function.</p>
  </blockquote>
  
  <p>Which I thought the Case statement would address but its not.
  Thanks in Advance for the help!</p>
  
  <p>My Code:</p>
  
  <pre><code>SELECT [ID]
    ,[T_File]
    ,[OrderNumber]
    ,[CustOrdNumber]
    ,[LineSeqNum]
    ,[SKU]
    ,[WebLineNum]
    ,CASE WHEN [T_File] LIKE '%.pdf%' THEN SUBSTRING([T_File],CHARINDEX('-', [T_File], CHARINDEX('-', [T_File]) + 1) -0,LEN([T_File]) - CHARINDEX('-', [T_File], CHARINDEX('-', [T_File]) + 1) - CHARINDEX('-', REVERSE(Rtrim([T_File])))) ELSE '' END AS PulledString    
  FROM [Portal].[dbo].[PA_URL]
  </code></pre>