# how do i generetare a unique identifier for 11 characters alphanumeric [Answered]RSS

## 8 replies

Last post Apr 13, 2015 02:15 AM by Edwin Guru Singh

Member

143 Points

423 Posts

### how do i generetare a unique identifier for 11 characters alphanumeric

how do i generetare a unique identifier for 11 characters alphanumeric ?

using sql table  a stored procedure

All-Star

49391 Points

9523 Posts

### Re: how do i generetare a unique identifier for 11 characters alphanumeric

You can take a look at the Unique Identifier NewID() to generate unique values.  This function generates a 36 char value. to reduce it to 11 you can use Left function like given below

`select LEFT(NEWID(),11)`

Thanks,
A2H
My Blog | Dotnet Funda

All-Star

21323 Points

4525 Posts

### Re: how do i generetare a unique identifier for 11 characters alphanumeric

You need to build a custom Logic to make your id unique. if you have a database, use integer field with identity will give you a unique value for all record. in this case, if you use int as the datatype, then you can store values up to  2,147,483,647, if you want much bigger,  use bigInt will give you the max value as 9,223,372,036,854,775,807.

If you want to generate alpha numeric, you need to write an algorithm that does the job for you. for e.g. two random letters and a random number between 0 and 999999999 padded to 9 digits can do the trick. Since you have the database, store the generated id in a column. Every time you generate an id based on your logic, check whether it exists in the table, if yes, generate another one, continue this process until you get one unique.

refer this

The real voyage of discovery consists not in making new landscapes, but in having new eyes

My Blog

Member

143 Points

423 Posts

### Re: how do i generetare a unique identifier for 11 characters alphanumeric

#### sreejukg

sreejukg

do you think this can be useful

https://codejotter.wordpress.com/2010/02/23/generate-random-and-unique-alphanumeric-code/

Contributor

2662 Points

660 Posts

### Re: how do i generetare a unique identifier for 11 characters alphanumeric

Create one function same as below and call this function when you need GUID:

```create function NextGUID()
returns int
as
begin
declare @lastval
int set @lastval = (select max(customerNumber) from Customers)
if @lastval is null
set @lastval = '00000000001'

declare @i int
set @i = @lastval + 1
return @i
end ```

Please mark the replies as if they help or unmark if not.

M Shoaib Waheed
MCSD & MCPD [Web Development 4.0]
http://shoaib.pk

Member

143 Points

423 Posts

### Re: how do i generetare a unique identifier for 11 characters alphanumeric

#### mshoaiblibra

Create one function same as below and call this function when you need GUID:

```create function NextGUID()
returns int
as
begin
declare @lastval
int set @lastval = (select max(customerNumber) from Customers)
if @lastval is null
set @lastval = '00000000001'

declare @i int
set @i = @lastval + 1
return @i
end ```

Your function will return integer always

I need alphanumeric  0 to 1 and from A to Z should always be capital

when i create a customer I want to call and  use a strored procedure than will return an alphanumeric unique that does not exist for any other customer

Member

143 Points

423 Posts

### Re: how do i generetare a unique identifier for 11 characters alphanumeric

#### A2H

You can take a look at the Unique Identifier NewID() to generate unique values.  This function generates a 36 char value. to reduce it to 11 you can use Left function like given below

`select LEFT(NEWID(),11)`

But i need to call this function every time i add a new customer so i need to make sure that the alphanumeric is unique and never used before

Member

143 Points

423 Posts

### Re: how do i generetare a unique identifier for 11 characters alphanumeric

#### sreejukg

check whether it exists in the table, if yes, generate another one, continue this process until you get one unique.

how can I do what you mentioned?

I was thinking to create a Defined table that contain a unique alphanumeric and every time I need to create a new Customer I retrieve from the defined table

and I add the lastId customer +1

Star

8536 Points

1376 Posts

Microsoft

### Re: how do i generetare a unique identifier for 11 characters alphanumeric

Apr 13, 2015 02:15 AM|Edwin Guru Singh|LINK

#### zubayba

how do i generetare a unique identifier for 11 characters alphanumeric ?
using sql table  a stored procedure