function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
niraj kumar 45niraj kumar 45 

SOQl Query to fetch Previous Previous 6 months records from today

I need to implenet SOQl query to fetch records form Sep 2016- to April 2106 from todays date for created records.
Suppose Today is May 2017 so need a SOQL query that fetches me the records from Sep 2016 to April 2016 which should be dynamic for createdDate records.
I can get the last 6 months records with this query
SELECT Id,CreatedDate  FROM content_description__c  WHERE CreatedDate = LAST_N_DAYS:180
how do i fetch the other previous 6 months data for created records?
 
v varaprasadv varaprasad
Hi Niraj,

Using Offset and Limit keywords you can retrieve old records.

Ex: 
last 180 days you received 50 reocords 
so you can write query like below :
SELECT name FROM Account  OFFSET 50
Hope this helps..

Thanks
Varaprasad
 
Shiva RajendranShiva Rajendran
Hi Niraj,
This will get you all the records between 6 -12 months from the current time.
Datetime dtTime=System.now();
date dt= date.newinstance(dtTime.year(), dtTime.month(), dtTime.day());
date dateBefore=dt.addMonths(-12);
date dateAfter=dt.addMonths(-6);
System.debug(dt.month() + '  ' +dt.year() +'  ' +dateBefore.month() + '  ' +dateBefore.year() +'  '+ dateAfter.month() +'  ' +dateAfter.year());
//System.debug(dateBefore.format() Date.f + '   '+dateAfter.format());
List<content_description__c> accs=[SELECT CreatedDate  FROM content_description__c where (createdDate>=:dateBefore and createdDate<= :dateAfter) order by createdDate asc];
for(content_description__c a:accs)
System.debug(a.createdDate.date())

Let me know if you need further help.
Thanks and Regards,
Shiva RV