November 9, 2011 at 8:25 am
Is there a way to combine the two columns into one?
R_ID int IDENTITY(1,1) PRIMARY KEY NOT NULL,
REPAIR_ID AS 'R' + RIGHT('00000' + CAST(R_ID AS VARCHAR(5)),5),
November 9, 2011 at 9:05 am
If you mean as part of the table definition, absolutely. Your syntax for the "computed column definition" seems to be correct. Are you having a problem with creating a table with such a column in it?
--Jeff Moden
Change is inevitable... Change for the better is not.
November 9, 2011 at 9:31 am
Yes. I am trying to create column REPAIR_ID as an identity with a leading "R". My current syntax works, but I would like to do this without creating the additional column. I figured it was possible, but I just can't seem to get the right syntax down.
November 9, 2011 at 10:37 am
bpowers (11/9/2011)
Yes. I am trying to create column REPAIR_ID as an identity with a leading "R". My current syntax works, but I would like to do this without creating the additional column. I figured it was possible, but I just can't seem to get the right syntax down.
Ok... I'm confused a bit. If you don't want the table to exist in the column, do you just want it in a query result? If so, use very similar syntax in a SELECT statement. Either of the following methods will do...
SELECT REPAIR_ID = 'R' + RIGHT('00000' + CAST(R_ID AS VARCHAR(5)),5)
FROM dbo.yourtable
;
SELECT 'R' + RIGHT('00000' + CAST(R_ID AS VARCHAR(5)),5) AS REPAIR_ID
FROM dbo.yourtable
;
--Jeff Moden
Change is inevitable... Change for the better is not.
November 9, 2011 at 12:32 pm
I want the unique id to include an "R" at the beginning of each record created:
R00001
R00002
R00003
Etc..
The following works, when creating the table, however I would like to consolidate the two columns shown into one if possible.
CREATE TABLE [REPAIR_PART]
(
R_ID int IDENTITY(1,1) PRIMARY KEY NOT NULL,
REP_ID AS 'R' + RIGHT('00000' + CAST(R_ID AS VARCHAR(5)),5),
Etc...
November 9, 2011 at 2:35 pm
I do not think I can accomplish this without having both columns. I will just persist my computed column, set it as the primary key, and roll on.
Thanks for taking the time to reply.
CREATE TABLE [REPAIR]
(
R_ID int IDENTITY(1,1) NOT NULL,
REPAIR_ID AS 'R' + RIGHT('00000' + CAST(R_ID AS VARCHAR(5)),5) PERSISTED PRIMARY KEY NOT NULL,
Etc...
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply