Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi All,

I have table in which i store division_code and division_name.
For each division code should be unique.
division code is auto generated and it starts from "EM_" + 1st letter of division.

Ex: Division Name: Test -> Division Code:EM_T
Ex: Division Name: Test1 -> Division Code:EM_Te
Ex: Division Name: Terminate -> Division Code:EM_Ter

How to generate unique code if some division starts with same letters?
Posted
Comments
Sergey Alexandrovich Kryukov 13-Aug-13 2:19am    
First of all: what is 1) purpose of it, 2) the scope of uniqueness?
—SA
Sweetynewb 13-Aug-13 2:50am    
Purpose- Requirement of system. i have to generate it..
Scope- if first letter is same then take first two letters of division name.

Sergey Alexandrovich Kryukov 13-Aug-13 2:53am    
This is not purpose. Why such requirements? Some rational.
This is not really the scope. Do you understand this notion? Scope could be some application or a single runtime of it (it means that on second run the IDs are allowed to repeat, and this is a very usual case), local computers, some set of computers on a network, whole world, etc.

You are not answering critical questions. Your solution depends on them.

—SA
Sweetynewb 13-Aug-13 2:56am    
Limitation is up to 3 letters.
But further what? that is what i am asking

I am doing this by doing substring of division name.
Sergey Alexandrovich Kryukov 13-Aug-13 3:00am    
You are not answering my questions, why should I answer yours? :-)
—SA

1 solution

I have asked a similar question[^] about unique values before and each of the answers have their own pros and cons. The question now will be how long do you want your unique values to be. Refer to the solutions on the link.


Edit:
With regards to your comment, why not just create the first characters of your division code, create the unique code, and then concatenate? Will the following be sufficient?

C#
string uniqueCode = System.Guid.NewGuid().ToString(); //E6ED1CBC-5FB3-4A1C-AD3C-5551F3836536
string divisionCode = "EM_T"; //or other value depending on your division name

// And then the final divisionCode will be divisionCode + uniqueCode, which in this case, will be
// EM_TE6ED1CBC-5FB3-4A1C-AD3C-5551F3836536
 
Share this answer
 
v2
Comments
Sweetynewb 13-Aug-13 2:47am    
okay but you are using inbuilt method. I need to create code using division name. Division name will be anything which user can enter.
walterhevedeich 13-Aug-13 2:58am    
I have updated my answer

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