How to cast a number to string with 0 prefix in sql server
sql, sql-server
Solution
So long as n is strictly less than 100, you can use
RIGHT(100+n,2)
In SQL Server 2012, you can also use FORMAT, and the following will work and also give you '100' for 100.
FORMAT(n,'00','en-US')
Problem
In sql server, if I have a number `n`, and `n>=0 and n<=100`, how to cast it into a string with sql? ``` 1 => '01' 2 => '02' ... 10 => '10' 99 => '99' ``` It is something like printf. ``` printf ('%2d", n); ```