title: How does SUBSTRING work with a VARBINARY field?
body: <p>Please see the DDL below:</p>
  
  <pre><code>CREATE TABLE #Person (id decimal(25,0))
  INSERT INTO #Person values (153121432060080000944513)
  </code></pre>
  
  <p>and the SQL statement below:</p>
  
  <pre><code>select substring(HASHBYTES('MD5', 
  CAST(id AS varchar(24))),1,4)
  from #person
  </code></pre>
  
  <p>I understand after reading the Hashbytes reference (<a href="https://msdn.microsoft.com/en-GB/library/ms174415.aspx" rel="nofollow">https://msdn.microsoft.com/en-GB/library/ms174415.aspx</a>) that a VARBINARY is returned.  How does the substring function work with a VARBINARY. The SUBSTRING function above returns the following:</p>
  
  <pre><code>0x05DDF8D9
  </code></pre>
  
  <p>This is more than four characters long.  I know that 0x denotes a VARBINARY.  Why are there more than four characters in the substring?</p>