How to create or what is the logic of
function which
Generate names for non registered users
using given name as string variable
using sql 2005
(like registering in hotmail ,yahoo)
How to create or what is the logic of function which Generate names for
Share
Are you trying to generate a unique username for a ‘user’ record, given their real name? Or email address? Is this so they can be tracked/stored in SQL Server (your question is not very clear)? The usual thing would be not to create a username for them (that’s one of the advantages you can use to entice the user to sign up, they get their own username).
Instead you could use the email address itself as the primary key, which will always be unique anyway: the username field would be nullable (allow NULLs), and only contain data if the user has signed up and picked a name.
If you wanted to display a username, or their email address if they don’t have a username, you could use COALESCE or ISNULL, for example
SELECT ISNULL(username, email_address)– which gives the value of ‘username’, unless it is null, in which case it gives the value of ’email_address’.Hope that helps – perhaps not the question you were asking but if you could rephrase or elaborate then we will be happy to help further.
Alternatively, I read your question again: are you trying to suggest usernames that are available, to the user? For example, if they enter:
Then you want to suggest KierenJohnstone, KierenJohnstone1, JohnstoneK, KJohnstone etc? (if they are available, that is).
If that is the case, you should write a method which generates possibilities, like this:
Then, you would see if those are available in the database:
You would need to write the IsAvailable() method, to take a string as a parameter and return a bool – ‘true’ if the username doesn’t already exist in the database.
This method will return a List of string objects, with suggestions: it will try the results from GeneratePossibleBaseUsernames, and if one of them isn’t available then it will try it with numbers 1 through 99 after it, e.g. if KierenJohnstone was taken it would suggest KierenJohnstone1.
You could also implement all of this within a SQL Stored Procedure. You will definitely want to double-check the name is available when you are inserting/update the record, or put a unique index on the username table to stop duplicates – in case two people use the same name as the same time!
Hope that helps.