Convert 6 digit letter to month and Year

  • Can any one share how to convert 6 digit number to mm/yyyy.

    Thanks in Advance.

  • Abhiram (2/27/2015)


    Can any one share how to convert 6 digit number to mm/yyyy.

    Thanks in Advance.

    What is contained in the 6 digit number?

    --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)

  • I have a column 062008 I want to display that number as 06/2008.

  • Abhiram (2/27/2015)


    I have a column 062008 I want to display that number as 06/2008.

    SELECT STUFF(yourcolumn,3,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)

  • Further on Jeff's fine solution, you may want to consider converting the column from (var)character data type to date data type. Not only does it take less storage (3 bytes), it also enables you to do calculation on the column.

    😎

    Extending the previous method to return a date data type

    SELECT CONVERT(DATE,'01/' + STUFF(yourcolumn,3,0,'/'),103)

    Alternative method without any string manipulation

    SELECT DATEADD(MONTH

    ,(((CONVERT(INT,yourcolumn,0) -1) % 10000) * 12)

    + (CONVERT(INT,yourcolumn,0) / 10000) -1

    ,CONVERT(DATE,'01/01/0001',103))

  • Thank you very much. It worked.

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

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