Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS SQL Server - Format Specification of Column

Status
Not open for further replies.

j1500

Programmer
Feb 15, 2006
10
US
I'm rather new to SQL Server. I hope you can guide me to the right solution.

PROBLEM: I need a column that will automatically generate the following data when a new record is created: P123 (Therefore the format is to start with the letter P followed by some auto increment number)

I have a primary key column that auto increments everytime a new record is added so 101, 102, 103, etc... I understand the primary key cannot contain letters so I created another column called acctnumber. I want the acctnumber column to automatically generate an account number in the format of P123 when a new record is inserted.

Again I'm just trying to find a way for the SQL server to automatically generate an account number when a new record is provided.

Thanks so much in advance for your help. I'm still learning.
 
primary keys can contain whatever you want -- numbers, letters, special characters -- as long as the values are unique

autonumbers, of course, can only be numbers

one solution to your problem is to use a computed column --

alter table daTable
add myacctnumber as 'P' + cast(acctnumber as varchar(9))

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top