0

In SQLSERVER my table has auto increment ID for each record, fairly straight forward. How do I set this ID number to be always 5-digit (I am never going to go over 99999 in this table)? So, record 1 is 00001, record 596 is 00596, 4871 is 04871,and so one... How can i set the 000001 formate on the Id Field.

iDevlop
  • 24,841
  • 11
  • 90
  • 149

1 Answers1

5
CREATE TABLE T (
   _ID int IDENTITY(1,1) not null,
   ID as RIGHT('0000' + CONVERT(varchar(5),_ID),5),
   /* other columns */
)

Although generally, you'd want to keep formatting concerns outside of the database - especially since IDENTITY columns shouldn't be shown to users.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448