Use NEWID() to Create a Unique Value in SQL Server

In SQL Server, you can use the NEWID() function to create a unique value.

More specifically, it’s an RFC4122-compliant function that creates a unique value of type uniqueidentifier.

The value that NEWID() produces is a randomly generated 16-byte GUID (Globally Unique IDentifier). This is also known as a UUID (Universally Unique IDentifier).

Example 1 – Basic SELECT Statement

Here’s a quick SELECT statement that returns NEWID():

SELECT NEWID() AS Result;

Result:

+--------------------------------------+
| Result                               |
|--------------------------------------|
| ab1b299b-9781-48d9-bedc-f238f6f5cc57 |
+--------------------------------------+

RFC4122 includes 5 versions (or sub-types). I can tell that this example uses RFC4122 version 4, because of the 4 in the relevant spot. Version 4 specifies that the GUID is randomly or pseudo-randomly generated. Other versions use other methods to generate the GUID.

When you look at all the examples on this page, you’ll notice that they’re all unique, but they all take the form: xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx, which means they’re all version 4 GUIDs, and therefore they’re all randomly or pseudo-randomly generated.

Example 2 – Using NEWID() with a Variable

Here’s an example of putting the value of NEWID() into a variable, then selecting it.

DECLARE @myguid uniqueidentifier = NEWID();
SELECT @myguid AS Result;

Result:

+--------------------------------------+
| Result                               |
|--------------------------------------|
| b6cfb25a-3f81-4e5a-a304-a4b44fb5dbeb |
+--------------------------------------+

Example 3 – Convert to String

Here’s an example of converting the variable value to a string, then printing it out.

DECLARE @myguid uniqueidentifier = NEWID();
PRINT 'Value: '+ CONVERT(varchar(255), @myguid);

Result:

Value: 9A229A64-6FE0-417E-B07D-78444EA7DA5B

Example 4 – As a DEFAULT Value in a Database

Here’s an example of using NEWID() as a default value in a database column.

USE Test;
CREATE TABLE Customer 
(
    CustomerId uniqueidentifier NOT NULL DEFAULT NEWID(),
    CustomerName varchar(70) NOT NULL,
);

INSERT Customer (CustomerName)
VALUES
    ('Peter Griffin'),
    ('Marge Simpson'),
    ('Borat Schwarzenegger');

SELECT * FROM Customer;

Result:

+--------------------------------------+----------------------+
| CustomerId                           | CustomerName         |
|--------------------------------------+----------------------|
| 6f7f606f-9ed3-48f3-b70e-90d901591203 | Peter Griffin        |
| 79b470df-a317-4359-92af-35cd6394eb5d | Marge Simpson        |
| ed59b667-4cd4-4849-b697-ec2410d7b7ff | Borat Schwarzenegger |
+--------------------------------------+----------------------+

In this example, I create a table that includes a uniqueidentifier column with its default value generated by NEWID(). I then insert data into that table. When I do this, I don’t specify the CustomerId value, so it uses the default value (which is generated by NEWID()). Finally, I select the contents of the table, which shows use the GUID that was generated with each row insert.

Example 5 – Explicitly Specified in the INSERT Statement

You can also explicitly use NEWID() when inserting the data.

Like this:

INSERT Customer (CustomerId, CustomerName)
VALUES
    (NEWID(), 'Bart Farnsworth'),
    (NEWID(), 'Bruce Norris');

SELECT * FROM Customer;

Result:

+--------------------------------------+----------------------+
| CustomerId                           | CustomerName         |
|--------------------------------------+----------------------|
| 6f7f606f-9ed3-48f3-b70e-90d901591203 | Peter Griffin        |
| 79b470df-a317-4359-92af-35cd6394eb5d | Marge Simpson        |
| ed59b667-4cd4-4849-b697-ec2410d7b7ff | Borat Schwarzenegger |
| a21e9a65-5efd-43ea-bd8b-8040b379617a | Bart Farnsworth      |
| 5ae0e501-3944-4be0-bf8a-ba964d8d13ed | Bruce Norris         |
+--------------------------------------+----------------------+

Here I use NEWID() to generate a unique ID for each row, and therefore the default value isn’t used. Either way, both are generated by NEWID() so we effectively get the same result – a randomly generated GUID.