简体   繁体   中英

Keyword for “Last Month” in SQL Server 2008

I have a query (pasted below), and I would like to make it so that people don't need to update the completed date range. I would like for it to automatically just get results from last month. So if it is run in February, for example, it will give me results for all completed items that meet my criteria for January. Can anyone think of a way to do that?

select  External_ID__c, 
        Ewrk_Tracking_Number__c,
        PIF_Branch_Name, 
        Distribution_Branch_Name, 
        Transaction_Type__C, 
        submitter_date__c, Completed_Date__C, 
        COUNT(External_ID__c)
from Business_Solutions_D.dbo.Reporting_SalesForce_AspireBaseData
where  PIF_Branch_Code = 977
    and Completed_Date__C >= '2015-01-01'
    and Completed_Date__C < '2015-02-01'
    and Delete_Flag__C = 'FALSE'
group by External_ID__c, 
         Ewrk_Tracking_Number__c, 
         PIF_Branch_Name, 
         Distribution_Branch_Name, 
         Transaction_Type__C,
         submitter_date__c,
         Completed_Date__C

There is no "keyword" for last month. You have to put that in your predicates.

Here is an example of how to get some date values for this.

select dateadd(MONTH, datediff(MONTH, 0, GETDATE()), 0) as BeginningOfThisMonth
select dateadd(MONTH, datediff(MONTH, 0, GETDATE()) - 1, 0) as BeginningOfPreviousMonth

If you want to see a number of other date routines here is an excellent blog post with quite a few of them. http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/

If you mean the last month prior to this one, you can do it in two steps: first, find the first day of the current month

@firstDayOfThisMonth = DATEADD(day, DAY(GETDATE())-1, GETDATE())

then subtract one month:

@firstDayOfLastMonth = DATEADD(month, -1, @firstDayOfThisMonth)

Then your query would be:

and Completed_Date__C >= @firstDayOfLastMonth
and Completed_Date__C < @firstDayOfThisMonth

Another way would be to query where the difference (in months) between Completed_Date__C and the current date is 1:

and DATEDIFF(Completed_Date__C, GETDATE()) = 1

You can do this with date arithmetic. One trick to get the first date of the month is to subtract the current day of the month from the date and add one day. SQL Server allows you to do this with + and - instead of dateadd() , on a datetime value. Of course, you need to remove the time component as well (using cast( as date) ).

The logic looks like this for the current month:

where Completed_Date__C >= cast(getdate() - day(getdate()) + 1 as date) and
      Completed_Date__C < dateadd(month, 1, cast(getdate() - day(getdate()) + 1 as date))

And like this for the previous month:

where Completed_Date__C >= dateadd(month, -1, cast(getdate() - day(getdate()) + 1 as date)) and
      Completed_Date__C < cast(getdate() - day(getdate()) + 1 as date)

This has the nice property that it is a sargeable as your original code, so it will take advantage of an index on the column, if appropriate.

You just need to have it do some date math to calculate it.

--Go to last day of prev month - 'Day' to account for varying month day counts
and Completed_Date__C >= GETDATE() - DATEPART(DAY, GETDATE()) - DATEPART(DAY, GETDATE() - DATEPART(DAY, GETDATE())) + 1
and Completed_Date__C < GETDATE() - DATEPART(DAY, GETDATE()) + 1

When you do additions on DateTimes + integer it assumes it day based addition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM