Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create column which increases from last index [duplicate]

I need to generate a column for InvoiceID. and I want to keep the formate of this column like this

INV0000001,
INV0000002,
.
.
.
.
INV0000010,
INV0000011,
.
. and so on. 

As you can see this column is increasing with last index. How can i do this.

I'm using SQL Server 2012.

I have searched, but couldn't find, how to increase a number like this.

like image 749
Amit Kumar Avatar asked Nov 18 '25 18:11

Amit Kumar


1 Answers

Try using computed column MSDN

CREATE TABLE Yourtablename
(
    ID int IDENTITY (1,1) NOT NULL,
    InvoiceID AS 'INV'+ right('000000'+cast(ID as varchar(20)),7) PERSISTED
);

SQLFIDDLE DEMO

For more info on why you need to make your computed column as persisted check here

like image 74
Pரதீப் Avatar answered Nov 20 '25 08:11

Pரதீப்



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!