Skip to main content

How to Create UUID in SQL Server

Complete guide with NEWID() and NEWSEQUENTIALID()

When you need a globally unique identifier for a SQL Server table, the built-in NEWID() function generates a random GUID in one call, with no extension or install required. Store it in the native UNIQUEIDENTIFIER type — 16 raw bytes instead of a 36-character string — so joins and index lookups stay fast as your tables grow. Below you'll find copy-paste ready T-SQL for generating a SQL Server UUID, casting a string back into a UNIQUEIDENTIFIER, and the sequential alternative that keeps a clustered index from fragmenting under heavy inserts. The uuid in postgresql guide covers uuid-ossp, pgcrypto, and built-in gen_random_uuid() across PostgreSQL versions.

Generate UUID for SQL Server

SELECT NEWID();
550E8400-E29B-41D4-A716-446655440000

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.

T-SQL
-- 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

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.

T-SQL
-- 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

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.

T-SQL
-- 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

Comments & Feedback

Share your experience or ask questions about this tool

Copied!