Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I Want to generate the Customer ID Like this CUST001...And second customer ID Like CUST002....
my code like this

C#
string str = "Cust";
for (int i = 001; i<= 999; i++)
 {
   string id = str + i;
         
 }

Please help me....
thanking you in advance....
Posted
Updated 18-Mar-13 10:32am
v4
Comments
joshrduncan2012 18-Mar-13 14:54pm    
We need a lot more info. What are you trying to do exactly? Where is your code? What is your question? Where are you stuck? This isn't a well-formed question.
Prasad Khandekar 18-Mar-13 15:03pm    
Hello Praveen,

The way I do it in my applications is to use a sequence and have a small function which takes 3 parameters the prefix, the serial and the desired length. Then it's all simple math. The number of zeros to be pad is equal to desired length - prefix length - serial length.

Regards,
Sergey Alexandrovich Kryukov 18-Mar-13 15:48pm    
Wow, no more then 999 customers! Why?!
—SA

Bad practice is bad practice ;(

If you have got table Customers without any identity[^] field (CustId), you're in trouble. Literally!
So, as soon as possible, alter table[^] and add column with identity. Any other solutions are partial.

Temporary, you can use ROW_NUMBER()[^], but this can't solve your problem.
SQL
SELECT 'Cust' + CONVERT(NVARCHAR(10),ID) AS CustID, [CustFName], [CustLName]
FROM (
    SELECT ROW_NUMBER() OVER(ORDER BY [CustLName]) AS ID, [CustFName], [CustLName]
    FROM Customers
    ) AS t1


Another, temporary solution, when you add new customer to your database, is to get the count of customers plus one.
SQL
DECLARE @id INT

SELECT @id = COUNT(*) + 1
FROM @cust

SELECT 'Cust' + CONVERT(NVARCHAR(10), @id ) AS LastCustomer

This solution is charged with errors too. Why? Remove one record to become convinced...

Summary
Alter table with identity field!
 
Share this answer
 
Comments
_Amy 18-Mar-13 23:47pm    
+5!
Maciej Los 19-Mar-13 2:57am    
Thank you, _Amy ;)
Leo Chapiro 19-Mar-13 1:47am    
+5!
Maciej Los 19-Mar-13 2:57am    
Thank you, du[DE] ;)
Try this:

C#
for (int i = 1; i < 1000; i++)
{
 string id = "Cust";
 id += i.ToString("000");

}
 
Share this answer
 
v2
Comments
praveenkatkuri 18-Mar-13 16:19pm    
it is display only the last number like CUST999 but i want to generate the id for each customer...cust001, cust002,.......cust999
Leo Chapiro 18-Mar-13 16:22pm    
lol - I don't think so, finally here is a loop ;)
[no name] 18-Mar-13 16:38pm    
What do you mean that it only displays the last number? Neither you or du[DE] are displaying anything anywhere. If you are displaying the result somewhere that is another problem that you need to figure out.
Maciej Los 18-Mar-13 17:59pm    
Technically... Solution is good (+5!), but it don't solves OP's problem ;)

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