2

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.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
Waheed
  • 10,086
  • 20
  • 53
  • 66
  • 2
    I feel the need to point out... a hexadecimal number is just like a decimal number. They're numbers. The only difference is how you decide to represent or display it. – Tom H Jun 01 '09 at 13:37
  • 1
    I agree with Tom's comment. There seems to be some sort of misunderstanding here... Why is it that you need a number in hexadecimal form? – Mark Canlas Jun 01 '09 at 13:52
  • Its user requirement that we should auto generate a HexaDecimal number as unique ID not an integer value. – Waheed Jun 02 '09 at 07:42

3 Answers3

2

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
  • 92,005
  • 12
  • 114
  • 115
2

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
  • 413,100
  • 91
  • 616
  • 614
1

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

CONVERT(varbinary(8), MyTable.MyId)
Robin Day
  • 100,552
  • 23
  • 116
  • 167
  • 1
    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 Jun 01 '09 at 12:21