Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hw to generate id like 0001,0002....0010
How to generate a column records like 0001, 0002, 0003...........,0010

For ex:
--------
id
--------
0001
0002
|
|
|
|
0010

Somebody can know this, pls help me...
Posted
Updated 28-Mar-22 1:09am
Comments
OriginalGriff 20-Jan-13 9:50am    
Is there any reason why you want leading zeros?
Surendra0x2 20-Jan-13 9:54am    
Sir,just i want to know can we generate these types of ids..
i did like but thisis not wrking-
create table autonumberid(id int identity,categoryid as('000'+convert(varchar,id)),productname varchar(20))

Try this

SQL
create table autonumberid(
	id int identity,
	categoryid as(right('0000'+CONVERT([varchar],[id],0),(4))),
	productname varchar(20)
)
 
Share this answer
 
1. If you don't care on sequential values - you can use auto increment field, and just format it, like


SQL
SELECT RIGHT ('0000'+ CAST (@Number AS varchar), 4)


2. If you care, you might want implement some business logic code in a function.
 
Share this answer
 
This worked in ms sql but How to use this into oracle
 
Share this answer
 
Comments
Richard Deeming 29-Mar-22 9:26am    
Your question is not a "solution" to someone else's question.

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