convert variable interger into string

  • My INT variable has got  values ranging from the length of 4 to 5.  on the conversion i need to pad the string with "00" to the left and if the length is 4 and pad it with "0" to the left if the length is 5.

    my variable is

    compound 

    2234

    9100

    9023

    54101

    60001

    2034

    how do i convert this into a string variable compoundid with the above condition?

     

  • Something like this?

    create table #x ( digit int not null )

    insert #x

    select 2234 union all

    select 9100 union all

    select 9023 union all

    select 54101 union all

    select 60001 union all

    select 2034

    go

    select digit, right('00' + cast(digit as varchar(6)),6)

    from #x

    go

    digit             

    ----------- ------

    2234        002234

    9100        009100

    9023        009023

    54101       054101

    60001       060001

    2034        002034

    (6 row(s) affected)

    /Kenneth

  • Create the above as a UDF to make your life easy

    Sathish

  • Thanks Kenneth,  it has worked.  i am greatful to ur advice.Great day.

  •  

    use this simple Query :

     

    select replace(str('2345',5),' ',0)

    NOTE :

    In place of  '2345' you can use your Column. but it should be convert into a varchar type.

     

    Regards ,

    Amit Gupta

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

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