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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning Auto Value

Status
Not open for further replies.

tester888

Technical User
Apr 5, 2005
7
GB
Hi there!
I am creating tables within my DB and was wondering if theres a way that i can assign a generated primary key which is not an autonumber such as 1,2,3.
I am using the following code and need to have some kind of auto character plus a number generated as a primary key for ComputerID. Any ideas?


CREATE TABLE Computers
(
ComputerID int IDENTITY NOT NULL PRIMARY KEY,
EngineerID varchar(10) NOT NULL

)
 
ok perhaps
C824637,
CI24314
something that is auto generated providing uniqueness to the column?
Is it possible to create this?
 
You can always create most anything.....

Once you have the first id, you could do something like:
Code:
INSERT myTable myID
 VALUES (SELECT 'C' + SUBSTRING(myID, 2, (LEN(myID)))+1 FROM myTable)

Maybe something like that.....might have to play with it a bit.

-SQLBill



Posting advice: FAQ481-4875
 
One mistake.....since you are adding 1 to the substring result, that makes it an integer, so you will need to convert that result back to VARCHAR before concatenating with 'C'.

-SQLBill

Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top