Click here to Skip to main content
15,891,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

for my application i need to increment 0001 to 0002 ,0002 to 0003,,,,,,???
Posted
Comments
[no name] 12-Sep-12 7:51am    
Using addition usually works
[no name] 12-Sep-12 7:55am    
can u please elobrate ur question
sk. maqdoom ali 12-Sep-12 8:03am    
i have to insert a field which increments by 1 in a table,
the value starts from 0001
in a procedure i am getting only 0001 by writing
SET @Number = @Number + 1
sk. maqdoom ali 12-Sep-12 8:04am    
it will continue from 0002 to 9999 when every insertion is made
[no name] 12-Sep-12 8:22am    
Declare @Number int
set @Number = 0001

insert into tbxyz values(number = @number + 1)

Hi,

the similar discussion you can found in the below link.
want to increment value by 1[^]

hope it helps.
 
Share this answer
 
v2
Hi,
Refer this link[^]. This will help you a lot.

Here I modified same thing for you.
Create a User Defined Function:
SQL
CREATE FUNCTION udf_ZeroPaddingAndConcat (
	 @Id INT,
	 @Length INT,
	 @PaddingChar CHAR(1) = '0'
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN (
	SELECT RIGHT(REPLICATE(@PaddingChar, @Length) + CAST(@Id as nvarchar(10)), @Length)
)
END
GO

Create the Table accordingly:
SQL
CREATE TABLE CustomIdentityTable
(
	 Id int IDENTITY(1,1) NOT NULL,
	 StringSequence as dbo.udf_ZeroPaddingAndConcat(CAST(Id as nvarchar(10)),6,'0'),
	 BookName nvarchar(250),
	 BookDescription nvarchar(max)
)

Now Insert Data:
SQL
INSERT INTO CustomIdentityTable VALUES('test', 'test')



--Amit
 
Share this answer
 
v2
1. you can store it as a string then fetch the character at 3rd place(0123).
2. Change this character to integer.
3. Incerement this number.
Programmatically you can do like this,

1. str.charAt(3);
2. int num = Integer.parseInt(str.charAt(3));
3. num++
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900