Reputation: 77
i have to generate invoice number like YYMM#### YY and MM are easy and we can get them using today date but how to add custom 4 digit numbers starts from 001 and end 999 i am using sql for storing invoice numbers
Upvotes: 0
Views: 3861
Reputation: 1503409
If you only have at most 999 invoices per month, you probably don't need to worry two much about the inefficiencies involved in two invoices in quick succession, so each you need to generate an invoice:
If you had to handle lots of invoices, potentially generated from lots of different clients, and didn't need contiguous invoice IDs, you could use a lo/hi algorithm with each client effectively "reserving" IDs. That sounds like overkill for this situation though.
Oh, and you should work out what you want to happen if there are more than 999 invoices in one single month...
1 You could potentially avoid the filtering here, and assume that everything else will follow the same convention, but personally I'd filter
Upvotes: 2
Reputation: 69574
If you want to do this in SQL Server side , you would need to create a SEQUENCE
object in sql server and you can do something like ..
CREATE SEQUENCE dbo.GetInvoiceNumber
AS INT
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 999
CYCLE
CACHE 10
;
SELECT CONVERT(VARCHAR(4), GETDATE(), 12)
+ RIGHT('0000'
+ CAST( NEXT VALUE FOR dbo.GetInvoiceNumber AS VARCHAR(3)),4)
The query will return values like
15080001
15080002
15080003
15080004
.......
Note
Are you sure your customer will never produce more than 999 Invoices in one month, I think the number should be fairly high just in case.
Upvotes: 2