Skip to content
Knowledge Base
Česky

Database · T-SQL

SQL: Padding numbers with leading zeros (15 -> 00015)

Robert Haken 1 min read Markdown ↓

You might find a better way, but how about:
[code language=”sql” light=”true”]
= REPLACE(STR(15, 5, 0), ‘ ‘, ‘0’)
[/code]

STR(num, length, decimals) function converts the number to string of given length (padded with spaces) and given number of decimals.

Alternative?

[code language=”sql” light=”true”]
= RIGHT(‘00000’ + @number, 5)
[/code]

Remember you should handle the formatting in presentation layer, but sometimes you might find this useful…

Robert Haken · HAVIT Software Architect, Founder at HAVIT, Microsoft MVP - ASP.NET/IIS.
Has 53 articles in the knowledge base.
All articles by this author →
HAVIT
Read next
Discussion

Leave a reply