Click here to Skip to main content
11,490,282 members (33,241 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL Server
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 20-Jan-13 4:46am
Comments
OriginalGriff at 20-Jan-13 9:50am
   
Is there any reason why you want leading zeros?
Surendra0x2 at 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))
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this

create table autonumberid(
	id int identity,
	categoryid as(right('0000'+CONVERT([varchar],[id],0),(4))),
	productname varchar(20)
)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

1. If you don't care on sequential values - you can use auto increment field, and just format it, like


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

2. If you care, you might want implement some business logic code in a function.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 570
1 Andy Lanng 233
2 OriginalGriff 188
3 Sascha Lefèvre 170
4 Maciej Los 160
0 Sergey Alexandrovich Kryukov 9,713
1 OriginalGriff 8,475
2 Sascha Lefèvre 3,504
3 Maciej Los 3,106
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100