Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to generate id in a specicfied format

Author  Topic 

imranabdulaziz
Yak Posting Veteran

83 Posts

Posted - 2008-01-15 : 01:59:50
Hi,
i am using sql server 2005.
i am making doctor maaster(which also store the data that are other then doctor like nurses etc)

i have to generates doctor id as dr00001 , next record dr00002 and so on

i am also using other details in the same table whose id should be ot00001 , ot00002 and so on ....

i would like to know is it possible to generates this type of structure automatically like identity column generates auto number.

can we design the table in such manner that it generates the id in specified manner.

please help.

thank you

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-01-15 : 02:12:17
you could have a regular identity column, and then a separate computed column somethign like this:


create table #mytable
(
id int identity(1,1) primary key
,name varchar(10) not null
,computedCol as 'dr' + right('00000' + convert(varchar,id), 5)
)

insert #mytable(name) select 'asdf' union all select 'qwert'
select * from #mytable
drop table #mytable


also have a look here: http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server


elsasoft.org
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-16 : 02:17:16
or
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=57069

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -