+ Start a Discussion
Pooja AroraPooja Arora 

SOQL Query Question

Hello, 
I have just started learning SOQL and Apex programming concepts.  I need some help with retrieving results using SOQL query.  
The requirement is: to get all the records where Planning date__c is less than 14 days. 
Any help is appreciated. 

Thank you. 
Navin Selvaraj23Navin Selvaraj23
Hi Pooja,


The first form is incorrect, you would do "SpecificDate__c >= :Date.Today().addDays(-14) AND SpecificDate__c <= TODAY"; without the second condition, you'd be querying from 14 days ago to all future dates.

The second form is also incorrect, you would want to do "SpecificDate__c = LAST_N_DAYS:14". Don't let the "=" sign fool you, LAST_N_DAYS:X is a date range, not just a single date. Using ">=" would select all days from 14 days ago through all eternity in the future, which is probably not your intent.

IF it helps, mark it as the best answer.

Regards,
Navin S
Ajay K DubediAjay K Dubedi
Hi Pooja,
Try this :
Select * from Account where WHERE date__c  = LAST_N_DAYS:14
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Raj VakatiRaj Vakati
You can do it like below 

Option 1 : 
 
SELECT CreatedDate FROM MyObject__c
WHERE CreatedDate = LAST_N_DAYS:1
ORDER BY CreatedDate DESC LIMIT 1

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

Option 2 :
 
date d = system.today().addDays(-30);
Account [] acc=  [select id from account where createdDate = :d];

 
Pooja AroraPooja Arora
Thank you Ajay!  That worked. 

Thank you all for taking time to respond to my question.