462

I want to create a varchar column in SQL that should contain N'guid' while guid is a generated GUID by .NET (Guid.NewGuid) - class System.Guid.

What is the length of the varchar I should expect from a GUID? Is it a static length?

Should I use nvarchar (will GUID ever use Unicode characters)?

varchar(Guid.Length)

PS. I don't want to use a SQL row guid data-type. I am just asking what is Guid.MaxLength.

Kols
  • 3,641
  • 2
  • 34
  • 42
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • 1
    Note: `Guid.NewGuid` has no implicit "string length"; It all depends on the format used in the [ToString](http://msdn.microsoft.com/en-us/library/97af8hh4.aspx) (The no-argument `ToString` uses "D" formatting). I prefer "B" as it's easier to "see that it's a GUID", but that's just familiarity and convention. –  Dec 16 '10 at 06:52
  • 11
    why not just save it as a 16byte uniqueidentifier? – Filip Cornelissen May 31 '13 at 10:14

7 Answers7

984

It depends on how you format the Guid:

  • Guid.NewGuid().ToString() = 36 characters (Hyphenated)
    outputs: 12345678-1234-1234-1234-123456789abc

  • Guid.NewGuid().ToString("D") = 36 characters (Hyphenated, same as ToString())
    outputs: 12345678-1234-1234-1234-123456789abc

  • Guid.NewGuid().ToString("N") = 32 characters (Digits and Letters only, no braces nor hyphens)
    outputs: 12345678123412341234123456789abc

  • Guid.NewGuid().ToString("B") = 38 characters (Braces)
    outputs: {12345678-1234-1234-1234-123456789abc}

  • Guid.NewGuid().ToString("P") = 38 characters (Parentheses)
    outputs: (12345678-1234-1234-1234-123456789abc)

  • Guid.NewGuid().ToString("X") = 68 characters (Hexadecimal)
    outputs: {0x12345678,0x1234,0x1234,{0x12,0x34,0x12,0x34,0x56,0x78,0x9a,0xbc}}

Vitox
  • 3,852
  • 29
  • 30
stevehipwell
  • 56,138
  • 6
  • 44
  • 61
77

36, and the GUID will only use 0-9A-F (hexidecimal!).

12345678-1234-1234-1234-123456789012

That's 36 characters in any GUID--they are of constant length. You can read a bit more about the intricacies of GUIDs here.

You will need two more in length if you want to store the braces.

Note: 36 is the string length with the dashes in between. They are actually 16-byte numbers.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Eric
  • 92,005
  • 12
  • 114
  • 115
44

The correct thing to do here is to store it as uniqueidentifier - this is then fully indexable, etc. at the database. The next-best option would be a binary(16) column: standard GUIDs are exactly 16 bytes in length.

If you must store it as a string, the length really comes down to how you choose to encode it. As hex (AKA base-16 encoding) without hyphens it would be 32 characters (two hex digits per byte), so char(32).

However, you might want to store the hyphens. If you are short on space, but your database doesn't support blobs / guids natively, you could use Base64 encoding and remove the == padding suffix; that gives you 22 characters, so char(22). There is no need to use Unicode, and no need for variable-length - so nvarchar(max) would be a bad choice, for example.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
9

I believe GUIDs are constrained to 16-byte lengths (or 32 bytes for an ASCII hex equivalent).

Ross Light
  • 4,769
  • 1
  • 26
  • 37
6

GUIDs are 128bits, or

0 through ffffffffffffffffffffffffffffffff (hex) or 
0 through 340282366920938463463374607431768211455 (decimal) or 
0 through 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 (binary, base 2) or 
0 through 91"<b.PX48m!wVmVA?1y (base 95)

So yes, min 20 characters long, which is actually wasting more than 4.25 bits, so you can be just as efficient using smaller bases than 95 as well; base 85 being the smallest possible one that still fits into 20 chars:

0 through -r54lj%NUUO[Hi$c2ym0 (base 85, using 0-9A-Za-z!"#$%&'()*+,- chars)

:-)

cnd
  • 1,689
  • 16
  • 14
  • 1
    In theory, yes. But with the huge hard disks of today, it's much more practical to use something like varchar(50). So if you store something like '1234ABC-ABCD-12AB-34CD-FEDCBA12' you don't have to go back and forth in translating it. What you are suggesting is slightly more CPU intensive than just reading/writing the value, which is what you want in practice. – LongChalk May 14 '18 at 12:19
6

22 bytes, if you do it like this:

System.Guid guid = System.Guid.NewGuid();
byte[] guidbytes = guid.ToByteArray();
string uuid = Convert.ToBase64String(guidbytes).Trim('=');
Qodex
  • 299
  • 3
  • 12
2

Binary strings store raw-byte data, whilst character strings store text. Use binary data when storing hexi-decimal values such as SID, GUID and so on. The uniqueidentifier data type contains a globally unique identifier, or GUID. This value is derived by using the NEWID() function to return a value that is unique to all objects. It's stored as a binary value but it is displayed as a character string.

Here is an example.

USE AdventureWorks2008R2;
GO
CREATE TABLE MyCcustomerTable
(
    user_login   varbinary(85) DEFAULT SUSER_SID()
    ,data_value   varbinary(1)
);
GO

INSERT MyCustomerTable (data_value)
    VALUES (0x4F);
GO

Applies to: SQL Server The following example creates the cust table with a uniqueidentifier data type, and uses NEWID to fill the table with a default value. In assigning the default value of NEWID(), each new and existing row has a unique value for the CustomerID column.

-- Creating a table using NEWID for uniqueidentifier data type.  
CREATE TABLE cust  
(  
 CustomerID uniqueidentifier NOT NULL  
   DEFAULT newid(),  
 Company varchar(30) NOT NULL,  
 ContactName varchar(60) NOT NULL,   
 Address varchar(30) NOT NULL,   
 City varchar(30) NOT NULL,  
 StateProvince varchar(10) NULL,  
 PostalCode varchar(10) NOT NULL,   
 CountryRegion varchar(20) NOT NULL,   
 Telephone varchar(15) NOT NULL,  
 Fax varchar(15) NULL  
);  
GO  
-- Inserting 5 rows into cust table.  
INSERT cust  
(CustomerID, Company, ContactName, Address, City, StateProvince,   
 PostalCode, CountryRegion, Telephone, Fax)  
VALUES  
 (NEWID(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL,  
 '90110', 'Finland', '981-443655', '981-443655')  
,(NEWID(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP',  
 '08737-363', 'Brasil', '(14) 555-8122', '')  
,(NEWID(), 'Cactus Comidas para Ilevar', 'Patricio Simpson', 'Cerrito 333', 'Buenos Aires', NULL,   
 '1010', 'Argentina', '(1) 135-5555', '(1) 135-4892')  
,(NEWID(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL,  
 '8010', 'Austria', '7675-3425', '7675-3426')  
,(NEWID(), 'Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens 532', 'Bruxelles', NULL,  
 'B-1180', 'Belgium', '(02) 201 24 67', '(02) 201 24 68');  
GO
Hunter
  • 21
  • 3
  • Somewhat more preferable to use an additional ID int identity(1,1) PRIMARY KEY A table without a primary key is inviting trouble. Suppose you have a million customers and you want a single line - WHERE CustomerID='xxx' - you wanna scan the entire table or seek it directly? This dual search - ID=524332 and CustomerID='xxx' is a very strong search. It's both very fast, and very secure (nobody can guess a GUID with brute force). – LongChalk May 14 '18 at 12:24