What is the string length of a GUID?

Shimmy Weitzhandler picture Shimmy Weitzhandler · Jun 9, 2009 · Viewed 329.3k times · Source

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.

Answer

stevehipwell picture stevehipwell · Dec 16, 2010

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