title: Check if unique value exists before updating in MSSQL
body: <p>I have a table containing a list of MAC addresses that in some cases has been padded to twelve digits and in other cases has not.</p>
  
  <p>ie: 00FF12345678 OR FF12345678</p>
  
  <p>There are cases where both values exist in this table.  I have a function that pads the value with the two zeroes and I can count the occurrence of these duplicates, but I don't have the time or patience to manually remove the duplicates or pad those that do not already have a record.</p>
  
  <p>I figured I could update the 10 digit values to their correct value with the following statement:</p>
  
  <pre><code>UPDATE [rs_DEVICES]
  SET [device] = dbo.pad_zero(12,[device])
  WHERE LEN([device]) &lt; 12
  </code></pre>
  
  <p>Of course this doesn't work because I need to only update those values that do not have an existing 12 digit record.  The unique constraint on this column stops me from doing this.</p>
  
  <p>I need some sort of IF EXISTS statement in the WHERE clause but I haven't been able to come up with anything that works. </p>
  
  <p>Using MSSQL Server 2008 R2. </p>
  
  <p>Thoughts?</p>