I need query for auto increment in string value-
I need XS000001,XS000009....XS000099...XS000999,XS009999,XS099999,XS999999
Please help me...
I need query for auto increment in string value-
I need XS000001,XS000009....XS000099...XS000999,XS009999,XS099999,XS999999
Please help me...
If this is for SQL Server (you're not clear on which actual database you're using), the best solution is to use
ID INT IDENTITY(1,1)
column to get SQL Server to handle the automatic increment of your numeric valueSo try this:
CREATE TABLE dbo.YourTable
(ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
AutoIncID AS 'XS' + RIGHT('000000' + CAST(ID AS VARCHAR(6)), 6) PERSISTED,
.... your other columns here....
)
Now, every time you insert a row into YourTable
without specifying values for ID
or AutoIncID
:
INSERT INTO dbo.YourTable(Col1, Col2, ..., ColN)
VALUES (Val1, Val2, ....., ValN)
then SQL Server will automatically and safely increase your ID
value, and AutoIncID
will contain values like XS000001
, XS000002
,...... and so on - automatically, safely, reliably, no duplicates.
But the question really is: what if your number range is used up? What do you do after have handed out XS999999
? We cannot answer that for you .....