I need a number that can never be duplicated.

T

Trint Smith

This will be a customer number that is automatically assigned. I want
it to be fast (so that it doesn't have to be checked for existance in
sql server) and impossible to ever reoccure. Could it be
date/hour/minute/second all as one integer?
Thanks,
Trint

.Net programmer
(e-mail address removed)
 
A

Al Reid

Where will you store this number and the other customer info? Will you ever use that number for validation or inserting detail
records??

Why not just let MSSQL generate the ID when you insert the customer data into the database?
 
C

Cor

Hi Trint,

Why not, have a look for Now.ticks for that

And do not forget to substract the ticks first from the point you did start
with your system till 0001 (is of course a fixed value).

I hope that was where you where looking for?

Cor
 
H

Herfried K. Wagner [MVP]

* Trint Smith said:
This will be a customer number that is automatically assigned. I want
it to be fast (so that it doesn't have to be checked for existance in
sql server) and impossible to ever reoccure. Could it be
date/hour/minute/second all as one integer?

You can use a GUID ('System.Guid').

;-)
 
K

Kevin L

If this number is to be stored in a SQL database anyway, you should
definitely use SQL to generate it. You say, however, that this is to be a
customer number so for it to have any real significance, it would have to be
stored somewhere, at some point, or you wouldn't need it?!
If you plan on using the customer number again (same customer purchases more
than once etc.), you will still have to "look it up".
Another note, if you ever have to enter this number by keying it in (ie -
typing), it is far better (more accurate) to type in a smaller integer than
a lengthy one.

To answer your question, yes and no, you could certainly use the combination
of Time related elements as you suggest as a moment in time will not be
repeated, but that number cannot be stored as an integer as it is too long.
You can store it as a Long Integer however.

Example: 02/12/2004 8:10:53
20040212081053
 
T

Trint Smith

Cor, I just tested now.ticks and that is nice...I know it will be
different every time...It's 18 digits long now, will that change? I
mean, in the next 100 years? How long will it stay 18 digits? The
reason I ask is because I need to know the size my sql server 2k column
size needs to be.
Thanks,
Trint

.Net programmer
(e-mail address removed)
 
J

Jay B. Harlow [MVP - Outlook]

Trint,
As Herfried stated, use System.Guid!

As System.Guid is a number that can be automatically assigned, that is fast,
and impossible to ever reoccur. (at least it is highly improbably that it
will ever reoccur).

I understand that System.Guid uses date/hour/minute/second plus some other
values to calculate a distinct value.

Hope this helps
Jay
 
A

Al Reid

I just have to ask... Why are you trying to generate a customer number external to MSSQL then worrying about the column size in the
database. Why not just insert the customer data and return the MSSQL IDENTITY? If you are concerned about the number of digits,
you can set the starting value.

I have done a lot of database work (MS ACCESS, MSSQL, ORACLE) and have never tried to generate a unique ID external to the database.
Just curious, what advantage do you see in doing that way?
 
J

Jay B. Harlow [MVP - Outlook]

Trint,
Do you have central daylight time where you are? More importantly will your
software run where there is central daylight time?

Now.Ticks will overlap an hour at least once a year, allowing duplicate
numbers!

Also, if you ever need to adjust the clock as the time is simply wrong,
Now.Ticks will overlap.

Again, I would recommend System.Guid, you can use either SQL Server or the
System.Guid structure itself to create a new one.

Hope this helps
Jay
 
C

Cor

Hi Trint,

The value of this property is the number of 100-nanosecond intervals that
have elapsed since 12:00 A.M., January 1, 0001.
I do not know how many customers you expect, but I think that you can start
at 1-1-2004 or something the ticks between that date and 1-1-0001 is the
amount of ticks you can subtract always.

Than you can in my opinion divide the result for a properiate expection of
new customers.

Or do you get every 100-nanosecond a new customer?

:)

Cor
 
C

CJ Taylor

Personally, I would use a GUID... It's based on time and has about a 1 in 1
trillion chance of being duplicated.

And its fast...
 
C

Cor

Hi Jay B,

I am not saying that this is the one I would, choise, on the other hand when
someone invent something like this, than I say always why not.

In Europe the daylight time change is always on a saterday on sunday between
02 and 03 o'clock (mostly we write in a 24 hour clock), that is 100 times
in a century, because only the turn back important.

I think that the change that there is a new customer on that time is really
very low (although it can happen).

I am talking for Europe of course I do not know if that time change in the
USA is on a daylight time in the middle of the week.

Cor
 
C

CJ Taylor

"
Or do you get every 100-nanosecond a new customer?

:)

Woudln't that be really nice if it were true? If you even charged a cent
for whatever you sold, you still would be loaded...
 
T

Trint Smith

I don't know how to:
insert the customer data and return the MSSQL IDENTITY.
And if Now.Ticks will overlap, I will have to use Herfried K. Wagner's
suggestion of System.Guid.
Thanks,
Trint

.Net programmer
(e-mail address removed)
 
T

Trint Smith

Will you show me a code example of System.Guid that will go in a string?
Thanks,
Trint

Net programmer
(e-mail address removed)
 
H

Herfried K. Wagner [MVP]

* Trint Smith said:
Cor, I just tested now.ticks and that is nice...I know it will be
different every time...It's 18 digits long now, will that change? I
mean, in the next 100 years? How long will it stay 18 digits? The
reason I ask is because I need to know the size my sql server 2k column
size needs to be.

Did you have a look at the documentation what the tick count is? It
will start to run every time the system is rebooted and it will restart
after some days because of an overflow.
 
H

Herfried K. Wagner [MVP]

* Trint Smith said:
Will you show me a code example of System.Guid that will go in a string?

\\\
Dim s As String = Guid.NewGuid().ToString()
///

'ToString' accepts a format parameter, so you can format the GUID the
way you want.
 
J

Jay B. Harlow [MVP - Outlook]

Trint,
Have you tried:

Dim g As Guid = Guid.NewGuid()
Dim s As String = g.ToString()

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top