Click here to Skip to main content
16,016,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i'd like to know one thing. i have a column called nos in a table. now i need to insert no s from 1 to 999 in tat column. if i insert 1 it should save as 001 and if i insert 34 it should save as 034. i want to declare this while specify the column s only. i dont want to run a seperate update statement.is there any way for this?
Posted
Updated 7-Aug-13 1:34am
v2
Comments
Maciej Los 7-Aug-13 7:45am    
Why do you want to add leading zeros? What if nos will be greater than 999?
ARUN K P 7-Aug-13 8:41am    
The requirement is like tat it should be 3 digits only. u cant insert more than 3 digits. tats why.

You cannot do this with integer or any numeric type - you need to use varchar or the database equivalent.
 
Share this answer
 
Comments
Maciej Los 7-Aug-13 7:46am    
Agree, but is it good practice? See my comment to the question.
Abhinav S 7-Aug-13 7:51am    
Yeah. This is perhaps something taht should be done at display (UI) level rather than at database level.
Try like

SQL
SELECT TOP 10 RIGHT('0000000'
+ CAST(TrnID AS VARCHAR(7)), 7) PaddedContactID
FROM tbl_Trn_Payments
GO
 
Share this answer
 
Comments
ARUN K P 7-Aug-13 8:44am    
ok mukesh. is there any way to specify that condition while declaring that column while creating table. like create table( no varchar(30)) . is there any way tat we could specify tat condition in create table statement?

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