Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2003
    Posts
    123

    Unanswered: Converting 7 (int) to '07' (varchar)

    Hi,

    I'm converting a query from Access to SQL Server.

    In this query I select from a column that contains numbers, the result I want is a varchar that is always 2 chars wide..

    Ie:
    7 should be selected as '07'
    12 should be selected as '12'

    In the Access-query it's rather nicely done with:
    Format(Str(mycolumn),"00")

    I could not find a way to make CONVERT do the same job... but I found that:
    LEFT('00',2-LEN(CAST(mycolumn as varchar)))+CAST(mycolumn as varchar)
    will do the job.

    But it feels like it could be done nicer.. any suggestions?

  2. #2
    Join Date
    Jan 2004
    Location
    Aberdeen, Scotland
    Posts
    1,067
    Im Pretty new to this myself but how about

    RIGHT('00' + CAST(MyColumn as Varchar(2)),2)

    Dave

  3. #3
    Join Date
    Jul 2003
    Posts
    123
    Thanks, I was too left oriented in my thinking :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •