views:

151

answers:

4

GUID is not an official data type in database. In our existing SQL Server design, the Uniqueidentifier type is used for GUID value. Now we are switching to Sybase database. Should we use varchar(36) to replace that Uniqueidentifier type?

I am still confused by GUID. I was told GUID is 16 bytes long but its character string is 36 characters in length. I must missed something.

+1  A: 

There is type uniqueidentifier.
varchar(36) should be equivalent in other databases.

To convert a uniqueidentifier value to a char data type:

DECLARE @myid uniqueidentifier
SET @myid = NEWID()
SELECT CONVERT(char(255), @myid) AS 'char'
GO

uniqueidentifier (Transact-SQL)

z-boss
yes, that is for SQL server what we have now. we want to change to Sybase.
5YrsLaterDBA
Then you need to convert before importing.
z-boss
+3  A: 

A GUID is actually an integer type - it's a 128 bit integer (16 bytes).

It's often represented as a string of 36 characters - but the actual value is a 128bit integer value.

Reed Copsey
ok, so we cannot use varchar(36) to take the GUID value because it is an integer type. but how to represent a 16 types integer type?
5YrsLaterDBA
+1. The number of characters can vary with representation, but always consists of 32 hexadecimal digits, but can sometimes include curly braces and hyphens.
Adam Robinson
Unfortunately, there is no 16byte integer type. You can use 2 BIGINT columns (8 bytes each), or a BINARY(16) to hold the data...
Reed Copsey
@Adam: Very true - the "36 character" is a common representation, adding {} and 2 dashes.
Reed Copsey
+2  A: 

Since an integer is 32 bits and a Guid is 128 bits, you could convert your Guid to four 32 bit integers (or two 64 bit integers). See here: http://stackoverflow.com/questions/1353555/represent-a-guid-as-a-set-of-integers

But it would probably be easier to keep it in the database as a string (or binary) if you don't have a native Guid type. Unfortunately, you won't have the ability to generate them on the database side like in SQL Server, so its use as a primary key would be limited.

Ilia Jerebtsov
+3  A: 

The reason that it's 36 characters is that a Guid is typically displayed as:

########-####-####-####-############

# = 1 hex character (0-9, A-F)
32 hex chars and 4 hyphens

A quick Google search found this Sybase site for newid that may help you out.

Austin Salonen
+1. Judging by that page, it seems that Sybase uses varchar(32) to store its Guid values.
Adam Robinson