Search code examples
sqlsql-server-2012sql-server-2000

SQL: Query integer column in specific format


I am trying to query a column of type (int) into a string of format (000). For example: If the column value was 1 then the output of the query should be 001


Solution

  • Try this:

    select right('000' + convert(varchar(3), intcolumn), 3) from yourtable
    

    Note that the output is of type varchar. If you will need this output as a number somewhere else, I would suggest doing the formatting in your UI code and keeping it as a number in the query.