Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

padding a number

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
how can i left pad a number with zero?
This is to generate a auto generated character key


 
Hi Kirtish

Check out the Replicate function in BOL. It sounds what you are looking for.

Say you wanted a surname to be padded with zeros and it had to be 20 digits in length.


Declare @ID varchar(20)
Declare @padded_ID varchar(20)

set @padded_ID= CONVERT(varchar(20), REPLICATE('0',20-LEN(@surname))) +CONVERT(varchar(20), @ID)


This will find the length of @ID and if it is shorted than 20, it will left pad @ID with 0's to make the length = 20. You have to convert it to varchar to use REPLICATE Hope This Helps
Bernadette
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top