How do you pad a string in SQL with leading Zeros (0)?
Sometimes you have numbers like:
0
2
20
30
350
If you want the numbers to all line in and pad with zeros then you can use the REPLICATE function in SQL Server to accomplish this:
REPLICATE('0', 3 - LEN(YOUR_FIELD))
The 3 in the statement above is how many characters should be. After running the REPLICATE, you should get this:
000
002
020
030
350
Plus, they are now easier to sort.
No comments:
Post a Comment