Returning two characters from a column

  • Hi

    I have a column that has values [1,2,3....10,11,13,14 ...]

    I want to return just two characters, e.g [01, 02, 03....10,11,12...]

    I concatenated a string before to

    '0'||CAST(m.d_depend as varchar(2)) as 'depend'

    but then I would get three character e.g [010,011,012...]

    How do I solve this

  • Use the RIGHT() function.

    Right('0' + col,2)

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • hoseam (11/13/2015)


    Hi

    I have a column that has values [1,2,3....10,11,13,14 ...]

    I want to return just two characters, e.g [01, 02, 03....10,11,12...]

    I concatenated a string before to

    '0'||CAST(m.d_depend as varchar(2)) as 'depend'

    but then I would get three character e.g [010,011,012...]

    How do I solve this

    That syntax seems like a combination from Oracle and SQL server.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (11/13/2015)


    hoseam (11/13/2015)


    Hi

    I have a column that has values [1,2,3....10,11,13,14 ...]

    I want to return just two characters, e.g [01, 02, 03....10,11,12...]

    I concatenated a string before to

    '0'||CAST(m.d_depend as varchar(2)) as 'depend'

    but then I would get three character e.g [010,011,012...]

    How do I solve this

    That syntax seems like a combination from Oracle and SQL server.

    That is definitely Oracle syntax. The || is the concatenation operator in Oracle.

    Regarding the problem, what happens if your numbers get up to 3 digits in length? What if you have [1,2,,,5,6] as one of your strings?

    For a great string splitter function, see the link in my signature. I don't know if it'll work in Oracle because I've never tried.

  • Ed Wagner (11/13/2015)


    Luis Cazares (11/13/2015)


    hoseam (11/13/2015)


    Hi

    I have a column that has values [1,2,3....10,11,13,14 ...]

    I want to return just two characters, e.g [01, 02, 03....10,11,12...]

    I concatenated a string before to

    '0'||CAST(m.d_depend as varchar(2)) as 'depend'

    but then I would get three character e.g [010,011,012...]

    How do I solve this

    That syntax seems like a combination from Oracle and SQL server.

    That is definitely Oracle syntax. The || is the concatenation operator in Oracle.

    But Oracle uses varchar2 instead of varchar and TO_CHAR() instead of CAST().

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

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

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