What is the string length of a GUID?
Asked Answered
I

7

473

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.

Ilbert answered 9/6, 2009 at 4:44 Comment(2)
Note: Guid.NewGuid has no implicit "string length"; It all depends on the format used in the ToString (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.Sagitta
why not just save it as a 16byte uniqueidentifier?Glamour
J
1015

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}}

It is possible to represent a Guids 16 bytes as a shorter string than shown above, but to do this you'd need to use an alternative formatter to the built in ToString() function.

Jaquiss answered 16/12, 2010 at 8:53 Comment(14)
@Shimmy - Look at the first one 'Hypenated, the same as default'Jaquiss
I have no idea what hypenated means :(Ilbert
@Shimmy - A hypen character - is used to seperate the groups.Jaquiss
Oh, then it's 'Hyphen' with an H (I was looking in the dictionary and wasn't able to find hypen)... ThanksIlbert
I'd like to add that a Guid is a 128-bit unsigned integer. You can also store it as a 16-byte array byte[16].Fanti
ps, there is another option: Guid.NewGuid().ToString("X") => 68 characters outputs: {0x12345678,0x1234,0x1234,{0x12,0x23,0x12,0x34,0x56,0x78,0x9a,0xbc}}Glamour
.NET reference page on formats: msdn.microsoft.com/en-us/library/97af8hh4.aspxHearne
It's 16 bits per character for a GUID in .NET which uses 16 bit words. That's 512 bits to store a 128 bit number. Not a good idea regardless of how cheap RAM is.Inapprehensible
the comment about 'digits only' with the "N" option is a bit tricky! You should read it as without braces & hyphensLaise
@Laise good point. I just edited the answer, as it's still pertinent.Monk
There's also base64-encoded guids (22 characters). EjRWeBI0EjQSNAAAEjRWeA for your example.Suspect
@Suspect I assume you mean that you can use a secondary encoder to get it down to 22 characters as I can't see a format option for this?Jaquiss
Yes. github.com/MiloszKrajewski/K4os.Text.BaseX has an out-of-the-box implementation for ShortGuid (url friendly, base64-encoded guid). And it also has a base85 encoder which you can use to further shorten to 20 characters. Unfortunately I think the default base85 codec doesn't use the charset from rfc1924 , but it does offer a constructor to create a codec with your own charset.Suspect
@Suspect that's a good example of an external formatter but this answer is focused on the built in .NET Guid functionality. I have added a passage about the ability to represent the Guids 16 bytes as an even shorter string if you want to use a custom formatter.Jaquiss
W
79

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.

Wentletrap answered 9/6, 2009 at 4:46 Comment(4)
I think one respresentation surrounds with {}, so that would mean a max of 38Centurial
I'm pretty sure you had it right the first time, Eric. guid.ToString() returns a string of length 36, with no braces.Cumming
Thanks for you two, what I will need is 36, I said I wanna store Guid.NewGuid.Ilbert
This is wrong for .NET; you only get 36 characters! You do get the braces (38 characters) for the C# visualizer, but not in code!Jaquiss
B
45

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.

Brieta answered 14/10, 2014 at 9:49 Comment(1)
why is uniqueidentifer fully-indexable but binary(16) is not ?Faithless
M
9

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

Mcdonnell answered 9/6, 2009 at 4:47 Comment(0)
M
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)

:-)

Millett answered 29/10, 2015 at 4:2 Comment(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.Maritsa
U
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('=');
Unnumbered answered 19/1, 2017 at 21:43 Comment(0)
C
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
Civilly answered 7/10, 2017 at 12:49 Comment(1)
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).Maritsa

© 2022 - 2024 — McMap. All rights reserved.