Click here to Skip to main content
15,881,725 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i need insert into dataase number with format like this:


0001
0002
0003
0005
Posted

You can define a column to be the primary key and then use your own logic to insert data into this key.
So long as the data unique, the server will not complain!

Generating this kind of string is easy. Use something like left('XXXXXXXXXXXX'+ rtrim(@str), @n) to format data.
 
Share this answer
 
Comments
Member 11280947 13-Oct-15 3:28am    
Thank You
Abhinav S 13-Oct-15 3:29am    
Mark as answered if it helped.
You can do this:

SQL
SELECT Right('0000' + CONVERT(NVARCHAR, 1), 4) AS NewFormat
SELECT Right('0000' + CONVERT(NVARCHAR, 2), 4) AS NewFormat
SELECT Right('0000' + CONVERT(NVARCHAR, 3), 4) AS NewFormat
SELECT Right('0000' + CONVERT(NVARCHAR, 4), 4) AS NewFormat
SELECT Right('0000' + CONVERT(NVARCHAR, 12), 4) AS NewFormat
 
Share this answer
 
Comments
Member 11280947 13-Oct-15 3:28am    
Thank You
Please use as

create table tblEmployee
(
ID varchar(4),
Name varchar(50)
)

Create proc SP_InsertEmployee
@ID int,
@Name varchar(50)
As
Begin
insert into tblEmployee(ID,Name) values (RIGHT('0000' + CAST(@ID as varchar), 4),@Name)
End


SP_InsertEmployee 5,'PANKAJ'

select * from tblEmployee
 
Share this answer
 
Hi,

Check this...

SQL
create table test123
(
para varchar(10)
)

insert into test123 values('00001')

select * from test123

--Output

para
-------
00001


Hope this will help you.

Cheers
 
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