Number padding in T-SQL
I saw a SQL Tip on padding numbers, which seemed really lousy.
The way I pad numbers is to use 'right', rather than 'left' and 'len'. And certainly not 'case'.
They wrote:
SELECT value,LEFT('000000',(6 -LEN(value ))) + CONVERT(VARCHAR,value) AS Formattedvalue
FROM #ValueTable
And I'm suggesting:
SELECT value,RIGHT('000000' + CONVERT(VARCHAR,value), 6) AS Formattedvalue
FROM #ValueTable
The way I see it - if you have something you want to appear on the RIGHT, with some unknown amount of stuff to the left, then it makes sense to use RIGHT. If I don't want it to break when the length of the value is too long, then a case statement will do the job.