title: Creating unique Barcode generator SQL query
body: <p>I'm trying to create a query to generate unique barcode number for each item, but yet I haven't come up with the logic behind it. (SAP B1)</p>
  
  <p>At this moment there are approximately 500 items in database without barcode. This query</p>
  
  <ol>
  <li>Should automatically assign barcodes to each existing item</li>
  <li>Should have a logic to generate new barcode number when I'm creating new.</li>
  </ol>
  
  <p>I thought that using Item <code>ROW_NUM()</code> function and <code>RIGHT</code> would be a good choice:</p>
  
  <p>Query will sort list by (with <code>ORDER BY</code>) Item creation time, get row number and convert it to barcode number with <code>RIGHT</code> function </p>
  
  <p>For Example:</p>
  
  <pre><code>select right('000000000000' + cast(Row_Number as varchar(12)), 12)
  </code></pre>
  
  <p>Barcode for fist item (first in list) will be<code>000000000001</code>
  for second <code>000000000002</code> etc.</p>
  
  <p>Is it possible?</p>
  
  <p>p.s. Presumably, this would be not useful for non-existent items. In Item creation process, row number will not be assigned yet.</p>