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

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

1 Answer 1

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
5
  • how to set ID here as primary key ?
    – Nad
    Apr 2, 2015 at 12:56
  • getting error as Conversion failed when converting the varchar value '11-' to data type int.
    – Nad
    Apr 2, 2015 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
    – Nad
    Apr 2, 2015 at 14:14
  • Select * from Tablename is my simple query
    – Nad
    Apr 2, 2015 at 14:49
  • 1
    @Nadeem Answer updated - I forgot to CAST the int as a varchar. Apr 2, 2015 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.