How to Generate a UUID in SQL Server
The simplest way to generate a UUID in SQL Server is the built-in NEWID() function, which produces a random, RFC 4122-compliant UNIQUEIDENTIFIER value with no extensions or setup required. For Node.js and Python PyMongo UUID patterns, the mongodb uuid guide has complete driver-specific examples.
-- Generate a random UUID directly
SELECT NEWID();
-- Output: 550E8400-E29B-41D4-A716-446655440000
-- Use it as a column default
CREATE TABLE Users (
Id UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
Email NVARCHAR(255)
);
Explanation
NEWID()is SQL Server's built-in function for generating a random GUID (UUID) — no extension or third-party install required.- The
UNIQUEIDENTIFIERdata type stores the value as 16 raw bytes internally, not as a 36-character string, so it's more compact and compares faster thanVARCHAR(36). - Setting
NEWID()as a columnDEFAULTmeans everyINSERTthat omitsIdgets a fresh GUID automatically, so application code never has to generate one itself. - Because
NEWID()produces a fully random value on each call, it's a good fit for public-facing identifiers where you don't want IDs to be guessable or sortable.
Convert a String to a UUID in SQL Server
A UUID sent as a string — from an API request or an application layer — needs to be converted to SQL Server's native UNIQUEIDENTIFIER type before it can be compared or stored efficiently. See the sqlite uuid page for TEXT vs BLOB storage comparison and mobile UUID patterns for iOS and Android.
-- CAST: standard ANSI SQL style
SELECT CAST('550e8400-e29b-41d4-a716-446655440000' AS UNIQUEIDENTIFIER);
-- CONVERT: SQL Server-specific, supports a style argument
SELECT CONVERT(UNIQUEIDENTIFIER, '550e8400-e29b-41d4-a716-446655440000');
Explanation
- Both
CASTandCONVERTparse the string and validate that it's a well-formed 32-character hex GUID, raising a conversion error immediately if it isn't. CASTfollows standard ANSI SQL syntax, making it the more portable choice if you might ever migrate the query to another database engine.CONVERTis SQL Server-specific and accepts an optional style argument for formatting on other data types — a capabilityCASTdoesn't offer, thoughUNIQUEIDENTIFIERconversions themselves don't use it.- Once converted, comparing the value against a
UNIQUEIDENTIFIERcolumn works exactly like comparing any other typed column, with no per-query casting overhead.
Other Options for Generating UUIDs in SQL Server
NEWID() covers most use cases, but SQL Server also ships NEWSEQUENTIALID() for tables where insert performance matters more than unpredictability.
-- NEWSEQUENTIALID() can ONLY be used as a column default
CREATE TABLE Orders (
Id UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID() PRIMARY KEY,
CustomerId UNIQUEIDENTIFIER NOT NULL
);
-- Calling it directly like NEWID() is not allowed:
-- SELECT NEWSEQUENTIALID(); -- fails
Explanation
- Unlike
NEWID(), which is fully random,NEWSEQUENTIALID()generates GUIDs that increase sequentially compared to the previously generated value on that computer. - Sequential GUIDs dramatically reduce index page fragmentation on the clustered index, since new rows land at the end of the index instead of being scattered randomly throughout it.
NEWSEQUENTIALID()can only be used as a columnDEFAULTconstraint — it cannot be called directly in aSELECTstatement or assigned to a variable, unlikeNEWID().- Because sequential values are predictable in ordering, avoid
NEWSEQUENTIALID()for identifiers that must not reveal creation order, such as public API resource IDs.