Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 816
Shimmy Weitzhandler Avatar asked Jun 09 '09 04:06

Shimmy Weitzhandler


2 Answers

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

like image 104
stevehipwell Avatar answered Oct 11 '22 11:10

stevehipwell


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.

like image 35
Eric Avatar answered Oct 11 '22 12:10

Eric