What is the string length of a GUID?

后端 未结 7 792
故里飘歌
故里飘歌 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:46

    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.

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