Inside the database table it should be stored as
ID001
ID002
ID003
ID004
the identity thing only works in int, we are looking at varchar here
Main Topics
Browse All TopicsI have a table with an ID as a Primary Key. an example if thess IDs are
Id001
Id002
Id003
..
.
..
and so on
How do i get the database to increment this value automatically when i add data to the other fields in the database
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
the best way is identity column as suggested by chaos_hooi
another solution:
you create a store procedure for insert.
e.g.
create procedure xyz
@param1,
.
.
@paramn
as
declare @nextid integer
select @nextid = convert(int,right(max(id),
insert into table values (@nextid, param1, ... , paramn)
note: you have to make sure that your max id is ID999
if you need bigger size, then you have to prepare from now on. e.g. ID99999
stored procedure will guarantee the id to be unique.
janithaj,
Please be aware that data does not have to be physically stored the way it is displayed. For example, if you create a genuine integer identity column, you can also create a computed (virtual) column to return the id in the format you want for display, for example:
CREATE TABLE tablex
(idNum INT IDENTITY(1,1),
id AS 'ID' + RIGHT('00'+CAST(idNum AS VARCHAR(3)), 3), ...
Then you can reference id in a SELECT, and you will get 'ID001', 'ID002', etc., even though you only have to store 1, 2, etc..
This provides one other advantage: if you later need a fourth digit, you can issue these commands:
ALTER TABLE tablex
DROP COLUMN id
ALTER TABLE tablex
ADD id AS 'ID' + RIGHT('000'+CAST(idNum AS VARCHAR(4)), 4)
and id will be changed everywhere to 'ID0001' rather than 'ID001' (provided, of course, you left enough extra space in the display field when needed).
You can use a trigger as follows:
(NOTE: my_table AND my_fld ARE YOUR TABLES AND FIELDS,
the table 'inserted' is a special table used by MS SQL for triggers.)
CREATE TRIGGER InsteadTrigger ON my_table
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO my_table ([ID], my_fld_one, my_fld_two, my_fld_thr)
SELECT 'db' +
CASE LEN(convert(VARCHAR(3),RIG
WHEN 1 THEN '00' + convert(VARCHAR(3),RIGHT([
WHEN 2 THEN '0' + convert(VARCHAR(3),RIGHT([
ELSE convert(VARCHAR(3),RIGHT([
END AS [ID] FROM my_table ), I.my_fld_one, my_fld_two, my_fld_thr
FROM Inserted as I
END
Good luck
janithaj:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
Business Accounts
Answer for Membership
by: chaos_hooiPosted on 2003-04-10 at 23:53:22ID: 8312000
Why didn't you use numbers and make the [ID] Column IDENTITY with 1 increment at the beginning?
Then, when you make queries from that tables, you can concatenate it with 'id' and 0(s) in front... It will make life easier...
Other than that, the only other thing I can think of is to use insert trigger... Maybe other people has better suggestions. ^_^