title: Uderstanding DECODE and ENCODE in MYSQL
body: <p>I needed to save id in such a way that would make it encrypted and so that it would take up exactly 8 characters.
  I did that using the following command:</p>
  
  <pre><code>SELECT encode(LPAD(id,4,0),'abc')
  </code></pre>
  
  <p>This command turns an id = 1
  into 0001 and then turns that into a code fe5ab21a</p>
  
  <p>How do I decrypt this code?</p>
  
  <p>Below is an example of a select and the result it generates</p>
  
  <pre><code>SELECT 0001
  , DECODE(ENCODE('0001', 'abc'), 'abc')
  , UNHEX(DECODE(ENCODE('0001', 'abc'), 'abc'))
  , ENCODE('0001','abc')
  , DECODE('fe5ab21a', 'abc')
  , UNHEX(DECODE('fe5ab21a', 'abc'))
  , HEX('0001')
  </code></pre>
  
  <p>The result:</p>
  
  <pre><code>1
  30303031
  0001
  fe5ab21a
  68d357a7005dcbe0
  NULL
  30303031
  </code></pre>