Click here to Skip to main content
16,017,238 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

Can anyone help me? I have created a table and with a primary key called "ID".


However, I want the "ID" be auto increment as well.

Here is my question, how can I do in the following format:


"PIC0001", "PIC0002", "PIC0003", ............ etc.


Anyone can help me?
Posted

Hi,

I think there is no need to maintain the Auto Increment Id with Characters & integers.

Simply create an Identity Column in your table which should be increment by 1.

Now while fetching the data from DB, you can use below query (eg.) for displaying the Character + Integer id

SQL
SELECT 'PIC' + RIGHT( '000000' + CAST(ID AS VARCHAR),7) FROM YOUR_TABLE


Here I am assuming, PIC as fixed string

You will get the required output.

Try above.

Hope this will help you.


Cheers
 
Share this answer
 
 
Share this answer
 
I don't have as such demo but let me know which is not clear to you or do you want me to
write code for you?
 
Share this answer
 
v2
Create A Function That will take AutoID as a parameter, now manipulate inside that function
as per your requirement .

Then create a separate column which will have that function in Table Definition & use this new
column.
 
Share this answer
 
Comments
Ankit Patel(AP) 11-Aug-14 1:59am    
U Have Demo ???

So Plz Upload Here

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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