Need to get YYYYMM from date ?

  • Hi Guys,

    Please help me to get YYYYMM from Getdate()?

    I have tried the below query.But it gives 20129 only not 201209 format?

    ANy help will be appreciated!!!!!!!!!

    Regards,
    Kumar

  • KumarSQLDBA (9/14/2012)


    Hi Guys,

    Please help me to get YYYYMM from Getdate()?

    I have tried the below query.But it gives 20129 only not 201209 format?

    ANy help will be appreciated!!!!!!!!!

    SELECT cast(year(getdate()) as char(4)) + right('0' + cast(month(getdate()) as varchar), 2);

  • SELECT CONVERT(CHAR(6), GETDATE(), 112);

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Million times thanks for the help from 'GSquared'.

    It got working .!!!!!!!!:-)

    Regards,
    Kumar

  • KumarSQLDBA (9/14/2012)


    Million times thanks for the help from 'GSquared'.

    It got working .!!!!!!!!:-)

    The next question is... do you understand why it works???

    --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 really appreciate the way how most of the frequent SQL gurus at SQL central try to feed the concept instead of just providing the answers.

  • But this one is not working for SELECT CONVERT(CHAR(6), '2012-09-15', 112);

    i mean When we provide a hard coded date.

  • balajisrm (9/15/2012)


    But this one is not working for SELECT CONVERT(CHAR(6), '2012-09-15', 112);

    i mean When we provide a hard coded date.

    Because that is not a hard-coded date. It is a hard-coded string defined as CHAR(10) being converted to another string defined as CHAR(6).

    Modify it to this:

    SELECT convert(char(6), cast('2012-09-15' As date), 112);

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • balajisrm (9/15/2012)


    But this one is not working for SELECT CONVERT(CHAR(6), '2012-09-15', 112);

    i mean When we provide a hard coded date.

    As mentioned, you need to convert that string to a date first.

    But, even better yet, follow the practice of hard-coding dates following the ISO standard. YYYYMMDD, without hyphens. That avoids issues with localization and conversion.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 9 posts - 1 through 8 (of 8 total)

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