• steve4134 (4/29/2013)


    Yep Lynn

    I just wanted to confirm that what I had wrote was indeed from last month threw the end of the month ran today. So if I ran this in May it would be April 1st to last day of April.

    Thanks again

    steve

    Just to be sure you actually understand, the code you posted will NOT always give you the last day of the previous month.

    If you don't believe me, try it yourself with various dates using the following code changing the value for the variable @ThisDate:

    declare @ThisDate datetime;

    set @ThisDate = '20130430 14:25';

    select

    @ThisDate CurrentDate,

    DATEADD(month, -1,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@ThisDate)-1),@ThisDate),101)) FirstOfPreviousMonth,

    DATEADD(month, -1,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@ThisDate))),DATEADD(mm,1,@ThisDate)),101)) [LastOfPreviousMonth-NOT]

    Modified for SQL Server 2005.