Solved

SQL Server Identity Column Leading Zeros

Posted on 2010-08-27
7
357 Views
Last Modified: 2012-05-10
Can I have my (int) identity column 6 numeric characters?

like this:
000001
000002
000003
000010
000011
000100
0
Comment
Question by:webdork
7 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 33546768
a identity field is numerical data type (int ... )numerical data types don't allow leading "0" ...so, you need 2 columns ... 1 being int/identity, and the second one being a computed column, using the other column, and formatting it as you need it:
alter table yourtable add formatted_key = right( '000000' + cast(your_identity_key as varchar(10)), 6)

Open in new window

0
 

Author Comment

by:webdork
ID: 33546812
I see... kinda.

I'm guessing if i run the statement above (with correct table and column names) it will create a new column called formatted_key and populate with correct values at this point in tome. How do i keep the new column updated as new records are added?
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33546818
...or just format the data in your application when you display the value.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 250 total points
ID: 33546823
leave it as identity (int) and use sql to get it formatted

select id, right('000000'+cast(id as varchar),6) as fid from myTable
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33546904
> it will create a new column called formatted_key
yes

> and populate with correct values at this point in tome.
no- a COMPUTED column is a "virtual" column that is only calculated when used. nothing is stored *

> How do i keep the new column updated as new records are added?
a COMPUTED column is "virtual", and only calculated when used, so whenever you use

select identity_key, formatted_key ....

formatted_key will automatically expand to "right( '000000' + cast(your_identity_key as varchar(10)), 6)" and give you the result desired.

* You can make a computed column stored by creating an index on it.
0
 

Author Comment

by:webdork
ID: 33546997
cyberkiwi:

Thank you so much for your detailed valuable response. I'd assigned points before I saw your input.

D
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33547597
webdork,

It is no problem.  Just wanted to make sure you understood.

Cheers
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question