I have a string that is up to 3 characters long when it\'s first created in SQL Server 2008 R2.
I would like to pad it with leading zeros, so if its original value w
Here is a variant of Hogan's answer which I use in SQL Server Express 2012:
SELECT RIGHT(CONCAT('000', field), 3)
Instead of worrying if the field is a string or not, I just CONCAT
it, since it'll output a string anyway. Additionally if the field can be a NULL
, using ISNULL
might be required to avoid function getting NULL
results.
SELECT RIGHT(CONCAT('000', ISNULL(field,'')), 3)