title: SQL Server : why is the 15th char of a GUID always 4?
body: <p>I'm just curious, I've always wondered why this is so.</p>
  
  <p>In an attempt to find out if I could create one without the character 4 at the 15th character, I ran this...</p>
  
  <pre><code>DECLARE @GUID AS NVARCHAR(36)
  DECLARE @COUNT AS INTEGER
  
  SET @COUNT = 0
  SET @GUID = CAST(NEWID() AS NVARCHAR(36))
  
  WHILE SUBSTRING(@GUID,15,1) = '4'
  BEGIN
      SET @COUNT = @COUNT + 1
      SET @GUID = CAST(NEWID() AS NVARCHAR(36))
  END
  
  PRINT 'Attempts : ' + CAST(@COUNT AS NVARCHAR(MAX))
  PRINT @GUID
  </code></pre>
  
  <p>As you might guess, this never actually ended for me. I had this running on a server all weekend.</p>
  
  <p>If NewID is supposed to always give a random ID, why is that 4 always there.</p>
  
  <pre><code>BC13DF1C-60FB-41C2-B5B2-8F1A73CF2485
  D790D359-AB3D-4657-A864-FA89FACB3E99
  DF1BBC0C-4205-48E8-A1B6-EA9544D7C6E5
  </code></pre>
  
  <p>Is the 15th position some kind of identify as to the system that generated the uniqueidentifier?</p>
  
  <p>In fact, the same thing happens with VB.net's <code>System.Guid.Newguid</code> function. Is the 4 a Microsoft only thing?</p>
  
  <p>Edit: Perhaps I should have also asked, are they actually unique? Can one rely one them being unique in an entire database? I know database systems based on the assumption these are guaranteed to be unique within the database. With several millions records in different tables... are any of them potentially the same?</p>