Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please i need help on trying to generate a random number to use as a unique identifier in sql database.I have a text box on a web application that a user is suppose to enter a username.Lets say a user enters his name as Micheal in this textbox.I want the computer to generate a unique number taking the the first two characters of the name entered in the textbox in this case which is (mi) and concatenate it with the current date and time as one long string.For example if the date is 10/20/2012 and time is 10:56pm i want the unique number to be Mi102020121056.The Mi iscoming from the string entered in the textbox which is micheal.Please help me out i will be very grateful for any help given.
Posted
Comments
joshrduncan2012 10-Oct-12 11:07am    
Can you please show us where you are stuck by providing the code you have so far and what kinds of output/error messages you are receiving?
fjdiewornncalwe 10-Oct-12 11:09am    
But what happens when two people submit with the same timestamp and just happen to have the same first two letters. You'll end up with a key constraint violation. Don't try to be too cute with your primary keys. In this case, I would use a normal identity auto-increment primary key field, but then just use a secondary id indexed field of your generated userid if you wish.
[no name] 10-Oct-12 11:12am    
Right +5
Sergey Alexandrovich Kryukov 10-Oct-12 12:21pm    
Right. Any play with primary keys could be a problem like the one you describe. Perhaps OP does not really understand how keys work in RDBMSs.
--SA
jamiebones 10-Oct-12 13:42pm    
I do I go about what you mentioned.Can you provide me with some sample code.I forgot to mention in my post that I am a novice.Any help is highly appreciated

1 solution

You can modify your code as per result retrieve from Select NewID()
It will give Unique ID

More About NewID()[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900