How to auto-increment something like C001,C002,C003..

Muhsinathk
Posted by Muhsinathk under Sql Server category on | Points: 40 | Views : 4756
CREATE TABLE [dbo].[Demo2](
[ID] [int] IDENTITY(1,1) NOT NULL,
[IDwithChar] AS ('C'+right('000'+CONVERT([varchar](10),[ID],0),(6))) PERSISTED,
[Name] [nvarchar](50) NOT NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Comments or Responses

Posted by: Gow.Net on: 6/7/2012 Level:Starter | Status: [Member] | Points: 10
hi nice but i need explanation for this Query [IDwithChar] AS ('C'+right('000'+CONVERT([varchar](10),[ID],0),(6))) PERSISTED
Posted by: Muhsinathk on: 6/8/2012 Level:Bronze | Status: [Member] | Points: 10
1) create a table Demo2

CREATE TABLE [dbo].[Demo2](
[ID] [int] IDENTITY(1,1) NOT NULL,
[IDwithChar] AS ('C'+right('000'+CONVERT([varchar](10),[ID],0),(6))) PERSISTED,
[Name] [nvarchar](50) NOT NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

2) Insert values into Demo2
3 Select * from Demo2

Login to post response