title: Select substring from a column in SQL
body: <p>How do I match a substring on another table?</p>
  
  <p>Table 1:</p>
  
  <pre>
  Reference | Value
  ----------+-------
  1         | 02.02.2011 07:07:00 498-123456-741
  5         | 123-789654-100
  5         | 123-789654-100
  </pre>
  
  <p>Table 2:</p>
  
  <pre>
  Reference | Code
  ----------+-------
  5         | 123-789654-700
  1         | 498-123456-100
  </pre>
  
  <p>I want to count the value from table one on table 2</p>
  
  <pre><code>select count(value) as count
  from table 1 join table 2 on substring(value,0,12)=substring(code,0,12)
  where reference='5' 
  </code></pre>
  
  <p>If it the value is present in Table 2 it gives me a count of 2.</p>
  
  <pre><code>select count(value) as count
  from table 1 join table 2 on substring(value,20,12)=substring(code,0,12)
  where reference='1'
  </code></pre>
  
  <p>So the first query works fine the second query when a value comes in like  <code>02.02.2011 07:07:00 498-123456-741</code> it doesn't compare it to table to even though that value is there, in Table 2 it will always be a substring of (0,12).</p>