Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you format a number into a string with padded zeros?

Very simply in SQL Server T-SQL parlance, how do you conver the number 9 to the string N'00009'?

like image 819
Jordan Avatar asked Jan 11 '11 18:01

Jordan


3 Answers

You can try

SELECT RIGHT('00000' + CAST(number AS NVARCHAR), 5)

The result will be a string, not a number type.

like image 98
bobs Avatar answered Dec 30 '22 13:12

bobs


If you're on SQL Server 2012 or later, you can also use the FORMAT function:

SELECT FORMAT(1, '00000') AS PaddedNumber

It nicely formats all kinds of stuff ...

like image 41
takrl Avatar answered Dec 30 '22 13:12

takrl


You can use this:

SELECT REPLICATE('0',5-LEN('9'))+'9'

The result is string

like image 33
Mohsen Avatar answered Dec 30 '22 13:12

Mohsen