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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Auto-incrementing with leading zeros
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

United Kingdom
11 Posts

Posted - 08/10/2006 :  08:57:26  Show Profile  Reply with Quote
Hi All,
I need to set up a kind of identity insert that gives an output in the format: 00001, 00002, 00003 etc. Is there a formatting option for this sort of output using normal identity insert features or do I need to write a function to insert these values (perhaps as text) each time a new record is created?
Sorry if this is really simple but it's only my 4th day in this job!

Flowing Fount of Yak Knowledge

5581 Posts

Posted - 08/10/2006 :  09:02:39  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
It will be better if you use IDENTITY all formatting (like padding zeros in the begining) at the front-end level if possible.

Harsh Athalye
"Nothing is Impossible"
Go to Top of Page

Starting Member

United Kingdom
11 Posts

Posted - 08/10/2006 :  09:42:32  Show Profile  Reply with Quote
Thanks Harsh, I thought of this first of all but the problem I have is that other people wil access the table from their own applications and I really need everyone to see a single unified view of the data without the need for formatting instructions.
Any other ideas?
Go to Top of Page

In (Som, Ni, Yak)

17689 Posts

Posted - 08/10/2006 :  10:07:42  Show Profile  Reply with Quote
use a computed column to format the zero padding

create table #table
	id	int identity(1,1),
	key_id	as right('00000' + convert(varchar(5), id), 5),
	col	int

insert into #table (col)
select	10

select * from #table

id          key_id     col         
----------- ---------- ----------- 
1           00001      10


Go to Top of Page

Starting Member

United Kingdom
11 Posts

Posted - 08/10/2006 :  11:25:29  Show Profile  Reply with Quote
Thanks KHTan, that's just what I needed!
Go to Top of Page

Premature Yak Congratulator

22864 Posts

Posted - 08/10/2006 :  12:11:56  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Use only identity column. If you use front end application, use format function to format the number


Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000