THE LAST DAY OF MONTH

  • liyaka

    SSC Veteran

    Points: 282

    Do you have any idea how to find the last day of the previous month?

    The select I have below works, however I am more than sure that there shoud be another less complex way to do it.

    select datediff(day, convert(varchar(4),

    year(getdate())) + '/' +

    convert(varchar(25),(month(getdate()) -1)) + '/' +

    convert(varchar(25),day(getdate())) ,

    convert(varchar(4), year(getdate())) + '/' +

    convert(varchar(25),month(getdate()))+ '/' +

    convert(varchar(25),day(getdate())))

    Thank you,

    Liya

  • jasoningram

    Ten Centuries

    Points: 1105

    Try this:

    print Dateadd(day, -1 * (day(getdate())), getdate())

    Unless there's a system proc that does it, I think this is about as clean as you'll get.

  • Andy Warren

    SSC Guru

    Points: 119655

    Pretty good Jason - though I think if you want a rock solid EOM function, you'd want to eliminate the time portion, easily done though.

    Andy

  • liyaka

    SSC Veteran

    Points: 282

    quote:


    Try this:

    print Dateadd(day, -1 * (day(getdate())), getdate())

    Unless there's a system proc that does it, I think this is about as clean as you'll get.

    Jason,

    Wow!!! Well done, great job! Thank you very much.

    Liya


  • nrbatcha

    SSC Rookie

    Points: 42

    quote:


    Do you have any idea how to find the last day of the previous month?

    The select I have below works, however I am more than sure that there shoud be another less complex way to do it.

    select datediff(day, convert(varchar(4),

    year(getdate())) + '/' +

    convert(varchar(25),(month(getdate()) -1)) + '/' +

    convert(varchar(25),day(getdate())) ,

    convert(varchar(4), year(getdate())) + '/' +

    convert(varchar(25),month(getdate()))+ '/' +

    convert(varchar(25),day(getdate())))

    Thank you,

    Liya



    Yours SQL Friend

  • nrbatcha

    SSC Rookie

    Points: 42

    Just do this SQL-Statement.

    Select Convert(DateTime,Convert(Varchar(4),DatePart(YYYY, GetDATE()))

    +'/'+Convert(Varchar(2),DatePart(mm,GetDate()))+'/'+'01') - 1


    Yours SQL Friend

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

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