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
like this:
000001
000002
000003
000010
000011
000100
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
...or just format the data in your application when you display the value.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> 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.
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.
ASKER
cyberkiwi:
Thank you so much for your detailed valuable response. I'd assigned points before I saw your input.
D
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
It is no problem. Just wanted to make sure you understood.
Cheers
ASKER
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?