title: Where Column like '%__2017%' is returning columns with _2017
body: <p>I have a table with a number of file names that have a date time stamp added to the file name. Unfortunately the process for creating the filenames is not consistent so I end up with names like,</p>
  
  <pre><code>RandomFilename__20170622_013633.csv
  Some_other_Filename__20170621_015423.csv
  YET-Another-Filename_20170605_010005.csv
  </code></pre>
  
  <p>Eventually I would like a select statement I would like to strip out the characters after filename and before the _timestamp and __timestamp. Ideally my select statement would remove the unwanted characters using LEFT() and CHARINDEX(). </p>
  
  <p>The below query is returning unexpected results</p>
  
  <pre><code>SELECT [Name]
  FROM FileList    
  WHERE NAME like '%__2017%'
  </code></pre>
  
  <p>I get filenames with just a single underscore as well. This is throwing off my charindex for '__2017' and '_2017'. </p>
  
  <p>Here is the current query I'm using to try and strip out the unwanted timestamp characters from the name column.</p>
  
  <pre><code>SELECT top 100 [Name]
    ,CASE 
      WHEN NAME like '%__2017%' then LEFT(NAME,CHARINDEX('_201',name) - 2)
      WHEN NAME like '%_2017%' then LEFT(NAME,CHARINDEX('_201',name) - 1)
    END AS ShortName
  FROM [dbo].[FilesHistory] 
  </code></pre>
  
  <p>Unfortunately this strips the filenames with a single underscore by 1 character too many, i.e. YET-Another-Filenam</p>