Link to home
Start Free TrialLog in
Avatar of webdork
webdork

asked on

SQL Server Identity Column Leading Zeros

Can I have my (int) identity column 6 numeric characters?

like this:
000001
000002
000003
000010
000011
000100
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of webdork
webdork

ASKER

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?
...or just format the data in your application when you display the value.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> 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.
Avatar of webdork

ASKER

cyberkiwi:

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

D
webdork,

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

Cheers