If you use Oracle Database or MySQL, you’re lucky enough to have the LPAD()
and RPAD()
functions, which allow you to pad a string with a given character (or characters) to its left and/or right.
However, SQL Server (or more precisely, T-SQL), doesn’t include these functions. So if you need some left padding, you’ll need to improvise.
This article presents four options for padding a number with leading zeros in SQL Server. So you can do stuff like turn 7
into 007
. Three of these options work on strings, so you can also apply padding to textual data.