Home All Groups Group Topic Archive Search About
Author
18 Aug 2005 8:58 PM
Yosh
Let's say I have a column that is a varchar(3).

What is the best way to pad result with 0's where length is < 3.

Column        Result
-----------        ---------
1                    001
12                  012
123                123

I hope this makes sense.

Thanks,

Yosh

Author
18 Aug 2005 9:05 PM
Alejandro Mesa
Try,

select c1, right('000' + c1, 3) as c2
from t1


AMB

Show quote
"Yosh" wrote:

> Let's say I have a column that is a varchar(3).
>
> What is the best way to pad result with 0's where length is < 3.
>
> Column        Result
> -----------        ---------
> 1                    001
> 12                  012
> 123                123
>
> I hope this makes sense.
>
> Thanks,
>
> Yosh
>
Author
18 Aug 2005 9:07 PM
Todd Beaulieu
One way: RIGHT('000' + CONVERT(VARCHAR, col), 3)

"Yosh" <Yosh@nospam.com> wrote in message
news:%23BuLIeDpFHA.2916@TK2MSFTNGP14.phx.gbl...
Let's say I have a column that is a varchar(3).

What is the best way to pad result with 0's where length is < 3.

Column        Result
-----------        ---------
1                    001
12                  012
123                123

I hope this makes sense.

Thanks,

Yosh
Author
18 Aug 2005 9:10 PM
KT
This is one way of doing it.  Replace @n & @x with your field names.

declare @n varchar(10)
declare @x int

set @n='000'
set @x=12

select substring(@n,1, len(cast(@x as varchar(10)))-1) + cast(@x as varchar(10))

  "Yosh" <Yosh@nospam.com> wrote in message news:%23BuLIeDpFHA.2916@TK2MSFTNGP14.phx.gbl...
  Let's say I have a column that is a varchar(3).

  What is the best way to pad result with 0's where length is < 3.

  Column        Result
  -----------        ---------
  1                    001
  12                  012
  123                123

  I hope this makes sense.

  Thanks,

  Yosh
Author
18 Aug 2005 9:14 PM
Yosh
Nice!

Thanks Everyone!

  "KT" <kt***@hotmail.com> wrote in message news:uPJD$kDpFHA.3376@TK2MSFTNGP10.phx.gbl...
  This is one way of doing it.  Replace @n & @x with your field names.

  declare @n varchar(10)
  declare @x int

  set @n='000'
  set @x=12

  select substring(@n,1, len(cast(@x as varchar(10)))-1) + cast(@x as varchar(10))

    "Yosh" <Yosh@nospam.com> wrote in message news:%23BuLIeDpFHA.2916@TK2MSFTNGP14.phx.gbl...
    Let's say I have a column that is a varchar(3).

    What is the best way to pad result with 0's where length is < 3.

    Column        Result
    -----------        ---------
    1                    001
    12                  012
    123                123

    I hope this makes sense.

    Thanks,

    Yosh

AddThis Social Bookmark Button