SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

marchawinslet
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!
Marcha

harsh_athalye
Flowing Fount of Yak Knowledge

India
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 column...do all formatting (like padding zeros in the begining) at the front-end level if possible.

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

marchawinslet
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

khtan
In (Som, Ni, Yak)

Singapore
17658 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
*/



KH

Go to Top of Page

marchawinslet
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

madhivanan
Premature Yak Congratulator

India
22765 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

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.08 seconds. Powered By: Snitz Forums 2000