How to find the Last Date of Previous Month

  • Hi All,

    How to find the Last date of Previous month.

    for Example,

    if you are giving getdate() as the value it should give the output as 31-Aug-2007.

    Can you please help me on this.

    Regards,

    Ezhilan

  • DECLARE @D datetime

    SELECT @D = GETDATE()

    SELECT DATEADD(day, - DAY(@D), @D)

  • Thanks it is working fine..

    I have used..this..

    select convert(datetime,convert(char,getdate()-datepart(dd,getdate()),103),104)

    Is it OK.

    Regards,

    Ezhilan

  • SELECT

    DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), -1)

     


    N 56°04'39.16"
    E 12°55'05.25"

  • Ezhilan, is there a reason for the two converts in your statement? They seem superfluous to me, as you are converting back into a datetime in the end, so the style codes aren't going to actually take affect. Additionally, you have two different style codes in the first place.

    I'd recommend you go with either Koji's or Peter's recommendations. Both should work fine, and Peter's method (zero-based date functions) is the one that is currently gaining in popularity. While it doesn't exhibit itself in this particular situation, that method can make for some very elegant (read short and concise) date manipulation in many cases.

  • Thanks David...

  • For more versatile uses of DATEADD/DATEDIFF combinations, see

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=86769

     


    N 56°04'39.16"
    E 12°55'05.25"

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

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