What is the string length of a GUID?

后端 未结 7 791
故里飘歌
故里飘歌 2020-11-29 15:15

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.

相关标签:
7条回答
  • 2020-11-29 15:26

    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.

    0 讨论(0)
  • 2020-11-29 15:26

    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
    
    0 讨论(0)
  • 2020-11-29 15:36

    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)
    

    :-)

    0 讨论(0)
  • 2020-11-29 15:43

    22 bytes, if you do it like this:

    System.Guid guid = System.Guid.NewGuid();
    byte[] guidbytes = guid.ToByteArray();
    string uuid = Convert.ToBase64String(guidbytes).Trim('=');
    
    0 讨论(0)
  • 2020-11-29 15:44

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

    0 讨论(0)
  • 2020-11-29 15:46

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

    0 讨论(0)
提交回复
热议问题