title: Get before last position of string in SQL Server
body: <p>I have a question about SQL Server. </p>
  
  <p>I want get data before last IN character position.</p>
  
  <p>Table <code>Stringposition</code>:</p>
  
  <pre><code>CREATE TABLE [dbo].[Stringposition]
  (
      [Name] [varchar](100) NULL
  ) ON [PRIMARY]
  GO
  
  INSERT [dbo].[Stringposition] ([Name]) 
  VALUES (N'PN MAIN DOOR IN'), (N'PN PASSAGE IN'),
         (N'INTestingIN'), (N'YENINFgh'), (N'inGENTRINin')
  GO
  </code></pre>
  
  <p>Expecting output data like below:</p>
  
  <pre><code>Name
  --------------
  PN MAIN DOOR
  PN PASSAGE 
  INTesting
  YEN
  inGENTRIN
  </code></pre>
  
  <p>I tried this query:</p>
  
  <pre><code>select 
      substring(name, 0, charindex('in', name, charindex('in', name) + 1)),
      name 
  from 
      [dbo].[Stringposition]
  </code></pre>
  
  <p>but this query not returning the expected result.</p>
  
  <p>Please tell me how to write a query to achieve this task in SQL Server.</p>