Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a uniqueid

Status
Not open for further replies.

craigward

Programmer
Nov 13, 2007
230
GB
Hi,

For a while now i have been researching the best option for a 16 alpha numeric key that i can create and use in my database but the best i found was a time stamp down to second year+month+day+hour+minute+seconds but this has now failed on me a few times.

I also found this server.createobject("scriptlet.typelib").guid which look great but it's like 40 characters long and i only have 16 characters to play with.

Does anyone have anything that works well that i can look at?

Thanks
 
Here's something that I just quickly wrote, you may want to go a different route, but it might point you in a new direction...

Code:
<%
for ndx = 1 to 10000
		' get NOW as a numeric value from the 1st Jan 2000 at midnight
		str = now() - cdate("1/1/2000 00:00:00")
		' split it by the decimal and only get the right side
		ra = split(str,".")
		str = ra(ubound(ra))
		' determine the number of digits (should always be 11, but check it just in case)
		lengthOfStr = len(str)
		' loop until a 16 character string is formed, 
		' basically adding 5 more random characters to the end
		' although it's not 100% bullet proof, the chances of two or more
		' people executing this script at the exact same time plus hitting
		' the exact same 5 number sequence are pretty slim
		for i = 1 to (16 - lengthOfStr)
			randomize
			' generate a random number between 1 and 33
			' letters such as I and O were left out to avoid confusion with the numbers 1 and 0
			random_number = int(rnd*33) + 1
			if random_number < 10 then str  = str & random_number
			if random_number = 10 then str  = str & "A"
			if random_number = 11 then str  = str & "B"
			if random_number = 12 then str  = str & "C"
			if random_number = 13 then str  = str & "D"
			if random_number = 14 then str  = str & "E"
			if random_number = 15 then str  = str & "F"
			if random_number = 16 then str  = str & "G"
			if random_number = 17 then str  = str & "H"
			if random_number = 18 then str  = str & "J"
			if random_number = 19 then str  = str & "K"
			if random_number = 20 then str  = str & "L"
			if random_number = 21 then str  = str & "M"
			if random_number = 22 then str  = str & "N"
			if random_number = 23 then str  = str & "P"
			if random_number = 24 then str  = str & "Q"
			if random_number = 25 then str  = str & "R"
			if random_number = 26 then str  = str & "S"
			if random_number = 27 then str  = str & "T"
			if random_number = 28 then str  = str & "U"
			if random_number = 29 then str  = str & "V"
			if random_number = 30 then str  = str & "W"
			if random_number = 31 then str  = str & "X"
			if random_number =  32 then str  = str & "Y"
			if random_number =  33 then str  = str & "Z"
		next
	response.write str & "<Br />"
next
%>

--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
This is what I use, remembering to first check the database to see if the string already exists and if it does then redo the function until it doesn't.

Code:
<%
Function createString(L)
Dim Randchar, strng, StrL
StrL= L
DO UNTIL Len(strng)=StrL
Randomize
Randchar = Int(Rnd*122)+1
IF (Randchar>47 AND Randchar<58) OR (Randchar>96 AND Randchar<123) THEN
strng=strng & chr(Randchar)
END IF
LOOP
createString = strng
End Function

RandString = createString(16)

'check if RandString is already in the DB...
'if so loop again
%>
 
Thank you for these ideas, i will now have a play with them.
 
your best bet would be to research true "timestamp" such as in DB2, which takes date+time down to the mili-second or absolute time in mili-seconds. it always works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top