ChatGPT解决这个技术问题 Extra ChatGPT

What is the string length of a GUID?

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.

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.
why not just save it as a 16byte uniqueidentifier?

A
Artem Novikov

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


@Shimmy - Look at the first one 'Hypenated, the same as default'
Oh, then it's 'Hyphen' with an H (I was looking in the dictionary and wasn't able to find hypen)... Thanks
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].
ps, there is another option: Guid.NewGuid().ToString("X") => 68 characters outputs: {0x12345678,0x1234,0x1234,{0x12,0x23,0x12,0x34,0x56,0x78,0x9a,0xbc}}
the comment about 'digits only' with the "N" option is a bit tricky! You should read it as without braces & hyphens
S
Shimmy Weitzhandler

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.


I think one respresentation surrounds with {}, so that would mean a max of 38
I'm pretty sure you had it right the first time, Eric. guid.ToString() returns a string of length 36, with no braces.
Thanks for you two, what I will need is 36, I said I wanna store Guid.NewGuid.
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!
P
Peter Mortensen

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.


why is uniqueidentifer fully-indexable but binary(16) is not ?
R
Ross Light

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


c
cnd

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)

:-)


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.
Q
Qodex

22 bytes, if you do it like this:

System.Guid guid = System.Guid.NewGuid();
byte[] guidbytes = guid.ToByteArray();
string uuid = Convert.ToBase64String(guidbytes).Trim('=');

H
Hunter

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

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).