9

I'm tying to retrieve data from the 2 months ago. I tried WHERE = LAST_N_MONTHS:2 but this returns data from last month too. So for now I want everything from 1st July - 31st July. This needs to update each month too.

On the same subject how do I get the same but for last month last year e.g. 1st Aug 16 - 31st Aug 16?

2 Answers 2

17

This should work for records from two months ago (i.e. - Today is 25th September, that means 1st July - 31st July):

WHERE CreatedDate = LAST_N_MONTHS:2 AND CreatedDate < LAST_N_MONTHS:1

And the second query for records from 13 months ago, which is the previous month of last year (i.e. - Today is 25th Sep. 2016, so this will return 1st August - 31st August 2015):

WHERE CreatedDate = LAST_N_MONTHS:13 AND CreatedDate < LAST_N_MONTHS:12
0

The last Last_N_Months would also give the last day of the previous month. Suppose I want to process previous Nov/21 data from today then the below query would also give me data of 31st October/21 which you don't want to process. So the best workaround to be more accurate is to use the exact date as given below:

CreatedDate >= 2021-11-01T00:00:00.000Z AND CreatedDate <= 2021-11-30T00:00:00.000Z Order By CreatedDate ASC

Now this will give you the exact record of that specific month. Like in this case it's giving me all the records only of Nov31.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .