title: How to do math with GUIDs in SQL Server
body: <p>I need to do some math with SQL Server GUIDs in a trigger and I'm having difficulty figuring out how to convert a <code>uniqueidentifier</code> to a <code>numeric(38,0)</code>.</p>
  
  <p>One potential problem: my understand is that both of these datatypes are 16-byte "integers". If I'm wrong here, please correct me.</p>
  
  <p>Otherwise, how would I go about this conversion? I've tried CAST and CONVERT and keep getting <strong>Explicit conversion from data type uniqueidentifier to numeric is not allowed.</strong> as an error message whenever I try. I'd really like to not have to parse each character and do hex math in a UDF to do this.</p>
  
  <p>Is this possible?</p>
  
  <p>Here's my script to repro this real quick:</p>
  
  <pre><code>DECLARE @guid uniqueidentifier
  SET @guid = NEWID()
  DECLARE @a numeric(38,0)
  SET @a = 2
  PRINT CAST(@guid AS numeric(38,0)) -- fails
  PRINT @guid / @a -- also fails
  </code></pre>