I had a 3 digit number in my program that needed to show leading zero's befor it was entered into the database as a string. For example:
1 would show as 001
21 would show as 021
837 would show as 837
Instead of play with loops and string lengths, I decided to use the RIGHT function:
DECLARE @endResult AS VARCHAR(3);
SET @endResult = '00' + CAST(@intValue AS VARCHAR(3));
SET @endResult = RIGHT(@rnc, 3)