2

I need to generate unique code like below in SQL Server

8E9560C,
2CAF902,
ADE4A86,
DF6A69E,
8074D34,

Anyone have idea how can I achieve it?

1
  • Why do you need these unique random codes ? What do you want to do ? Commented May 31, 2014 at 10:55

2 Answers 2

3

Have you tried this ?

select newid()

or if you want only 7 chars :

select left(newid(),7)

Sign up to request clarification or add additional context in comments.

5 Comments

It gives me guid. I need 7 characters code containing alphanumeric
did you read my answer ?
sorry Royi. It gives me desire output.
Hi Royi, I have tested this for millions of records and it is found that code is getting duplicated.
You should check that there is no previous match
2

NEWID() produces a v4 GUID. In that GUID scheme, the first 8 bytes can be any hexadecimal digit 0-F and will be composed entirely of randomly-generated data. That's not guaranteed to be unique; in fact no v4 GUID is guaranteed to be unique, it's just that the random bits (112 of 128) can represent one of 5.19 decillion numbers, so the odds of any two of them matching in the same system is infinitesimal. With only the first 8 bytes, you'll only have 2^32 combinations, which may still seem like a lot (1 in 4 billion) but by the birthday problem, after a scant 77,000 have been generated you have a 50-50 shot at generating a duplicate.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.