How to turn 3 into ''03'' ?

  • Is there a nice way to turn, eg, 3 into '03', without a bunch of casting, or a UDF (which I fear will slow things down?

    Eg, a nice builtin of which I'm ignorant, ZeroExtend(int base, int width)

    ?

  • How about ...

    declare @y int

    set @y = 3

    select substring(convert(varchar(3), @y + 100),2,2)

    It's not a builtin, but it doesn't look too painful.


    And then again, I might be wrong ...
    David Webb

  • select right('00' + convert(varchar(2), 3), 2)

  • I figured out a replicate call, but it required wrapping it in a case to avoid passing a negative to the replicate. That right/convert combo is more succinct. Thanks.

  • Or...

    SELECT REPLACE(STR(3,2),' ','0')

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply