Left-padding a column

I have had many situations where there needs to be a left padded column in sql. Usually it's a number that needs to have pre-pended 0s. Here is a very simple way. I will list the formula and then give an example:

select right('<X>0'+cast(@var as varchar(X)),X)

The bold sections (the X and the number of 0s) need to be the same. So given the above formula, if I need a column padded out 10 places, I have 10 0s which means the max size of the field will be 10 and I need to make both Xs 10:

select right('0000000000'+cast(@var as varchar(10)),10)

If the max column size is 4, the above formula would be:

select right('0000'+cast(@var as varchar(4)),4)

If you need to do this in sql 2000 simply replace the cast with a convert:

select right('<X>0'+convert(varchar(X),@var),X)