SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


16 digit unique number


16 digit unique number

Author
Message
Paresh Prajapati
Paresh Prajapati
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3295 Visits: 464
Can have script to generate 16 digit unique number which contain both character/Number..
which should be not duplicate in future also.

_____________________________________________________________________________________________________________
Paresh Prajapati
Arrow +919924626601
http://paresh-sqldba.blogspot.com/
LinkedIn | Tweet Me | FaceBook | Brijj
krayknot
krayknot
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3126 Visits: 534
Paresh Prajapati (1/20/2009)
Can have script to generate 16 digit unique number which contain both character/Number..
which should be not duplicate in future also.


declare @random varchar(50)
set @random = newid()
print (@random)

select substring(@random,1, 16)

kshitij kumar
kshitij@krayknot.com
www.krayknot.com
colin.Leversuch-Roberts
colin.Leversuch-Roberts
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19723 Visits: 715
I think you're asking too much here and I'm not too certain this is an easy task in T-SQL. A guid will give you 32 chars which should be unique, although you'll still need a constraint as I have encountered duplicate guids a few times.
I'd go for guid and save yourself agony, although I'd actually go for an int being smaller.

The GrumpyOldDBA
www.grumpyolddba.co.uk
http://sqlblogcasts.com/blogs/grumpyolddba/
Paresh Prajapati
Paresh Prajapati
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3295 Visits: 464
krayknot (1/20/2009)
Paresh Prajapati (1/20/2009)
Can have script to generate 16 digit unique number which contain both character/Number..
which should be not duplicate in future also.


declare @random varchar(50)
set @random = newid()
print (@random)

select substring(@random,1, 16)


It may possible substring(@random,1, 16) will be duplicate in future...

_____________________________________________________________________________________________________________
Paresh Prajapati
Arrow +919924626601
http://paresh-sqldba.blogspot.com/
LinkedIn | Tweet Me | FaceBook | Brijj
colin.Leversuch-Roberts
colin.Leversuch-Roberts
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19723 Visits: 715
yes there's a stong chance anything which truncates a guid or uses the rnd function will generate a duplicate.

The GrumpyOldDBA
www.grumpyolddba.co.uk
http://sqlblogcasts.com/blogs/grumpyolddba/
Lowell
Lowell
SSC Guru
SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)

Group: General Forum Members
Points: 123194 Visits: 41432
how about an Identity using a bigint column that starts with the first 16 digit number ?

create table #example( bigintID bigint identity(1000000000000000,1) primary key,
morestuff varchar(30) )



also why must it be 16 digits?
::editing i just saw it needs to be letters and numbers...i have an example, but it is still based off of an identity column::

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Lowell
Lowell
SSC Guru
SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)

Group: General Forum Members
Points: 123194 Visits: 41432
i misread the original post;
here's a sample from my snippets where someone wanted a unique alphanumeric, in order, ie AAA001 thru ZZZ999;
the nubmer gets generated based on an identity.\
you could do the same, and just pad it so that it is 16 chars:


DROP TABLE X
CREATE TABLE X(XID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CHECK ((XID> 0) AND (XID < 676000)),
XCALCULATED AS   CHAR((XID/26000)%26+65) --1st Letter
      
+CHAR((XID/1000)%26+65)  --2nd Letter
      
+REPLACE(STR(XID%1000,3),' ','0'), --The 3 digit numeric part
SOMEOTHERCOL VARCHAR(30)
)


INSERT INTO X(SOMEOTHERCOL) VALUES('WHATEVER')
SET IDENTITY_INSERT X ON
INSERT INTO
X(XID,SOMEOTHERCOL) VALUES(675999,'MORESTUFF')
SET IDENTITY_INSERT X OFF
SET IDENTITY_INSERT
X ON
INSERT INTO
X(XID,SOMEOTHERCOL) VALUES(676000,'MORESTUFF')
SET IDENTITY_INSERT X OFF
SELECT
* FROM X

XID XCALCULATED SOMEOTHERCOL
1 AA001 WHATEVER
675999 ZZ999 MORESTUFF





Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Matt Miller (4)
Matt Miller (4)
SSC-Forever
SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)

Group: General Forum Members
Points: 46666 Visits: 19257
The only way you're going to ENSURE that it's unique is to keep track of all of the ones previously generated. Use whatever methodology you like to gen it, then store it in table: if it's already been gen'd then do it again.

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Dutta, Amartha
Dutta, Amartha
Old Hand
Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)

Group: General Forum Members
Points: 329 Visits: 110
Dear Friend,

How about looking into manipulating the current system date & Time.
Use GetDate(), Remove the spaces and append with some other characters / digits of your choice (Based upon some logic, if you find comfortable).

Lets Say GetDate() Returns :- 2009-01-21 19:06:26.777
Trim The "-" Then 20090121 (8 Characters)
Remove ":" Then 190626 (6 Characters)
Remove "." Then 777 (3 Characters) Take First 2

So the Key 2009012119062677 = 16 Digits.
It can't be duplicated never ever - As the same date won't repeat back again.

Take Care
Bye
Happy SQLing...
LutzM
LutzM
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38133 Visits: 13559
Hi,

using the current system time to get a unique value is dangerous if the underlying server follows DST (daylight saving time).
What happens if you generate a key around the switch from DST back to normal time?
In this case you end up with one hour every year where you can have duplicates...

If you don't want to store all the previous data in a table, you could use a combination of current date and a generated random code. Store the random part in a table and truncate it every midnight.

Regards
Lutz



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search