0

I have a issue,

I want to set the ID column, where the ID should start from 11-2 then it should auto- increment by 1 like

11-3, 11-4.

I am using sql-server 2008 R2. Please help

  • Will the prefix always be 11- or will it eventually turn into 12-, 13-, etc..? – LowlyDBA Apr 2 '15 at 12:34
  • @John: For now, let say it will be always 11 – hud bhaisahab Apr 2 '15 at 12:41
1

Use an IDENTITY column for the incrementing ID value, then prefix it with 11- as a computed column.

CREATE TABLE [Table2]
    (
    ID int IDENTITY(1,1),
    ID_pretty AS ('11-' + CAST(ID AS nvarchar))
    CONSTRAINT pk_ID PRIMARY KEY (ID))
GO
  • how to set ID here as primary key ? – hud bhaisahab Apr 2 '15 at 12:56
  • getting error as Conversion failed when converting the varchar value '11-' to data type int. – hud bhaisahab Apr 2 '15 at 13:04
  • I added two more columns to it, one is varchar and another one is int. When I run the select query, I got the above mentioned error – hud bhaisahab Apr 2 '15 at 14:14
  • Select * from Tablename is my simple query – hud bhaisahab Apr 2 '15 at 14:49
  • 1
    @Nadeem Answer updated - I forgot to CAST the int as a varchar. – LowlyDBA Apr 2 '15 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.