query to find out last date of the previous month

  • I am trying to write a query to find the last day of the previous month.

     

  • Substitute the date you have for getdate()

    select

    dateadd(d,-1,convert(datetime,convert(varchar(2),datepart(mm,getdate())) + '/01/' +

    convert(varchar(4),datepart(yy,getdate()))))

     


    And then again, I might be wrong ...
    David Webb

  • Select

    DateAdd(dd,-1,Convert(datetime,Convert(varchar(4),Year(GetDate())) + '-' + Convert(varchar(4),Month(GetDate())) + '-01'))

  • Short, simple, nasty fast because there are no VARCHAR conversions...

    SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)-1

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

  • Very nice!  That's so slick!


    And then again, I might be wrong ...
    David Webb

  • Same result with time..

    select getdate() - datepart(day,getdate())

  • Thanks Much all..

    Kavita

     

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

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