views:

394

answers:

3

I want to generate unique hexadecimal numbers in SQL. How can I do this?

If you know how to generate in c# please also include that.

+2  A: 

In SQL Server: newid()

In C#: System.Guid.NewGuid()

These get you GUID's, which are unique, hexidecimal numbers; however, they tend to have dashes (-) in the middle of them, so you may have to do a bit of string parsing to get what you want. Other than that, though, this should work for you.

Eric
+1  A: 

You could use an integer IDENTITY column and then a conver that to hex using

CONVERT(varbinary(8), MyTable.MyId)
Robin Day
Should be varbinary(8). Also, question for the OP: This gets you a unique number for that table. If you just want a unique hex number to identify a row in a table, go with this one--lot easier to manage and type in for manual queries.
Eric
+2  A: 

SQL Server:

SELECT  NEWID()

Oracle:

SELECT  SYS_GUID()
FROM    dual

MySQL:

SELECT  UUID()

In PostgreSQL, you'll have to use an external function, though it has UUID type.

Quassnoi