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
pradyprady 

Soql query to retrieve opp between two dates

Hi,

 

I am trying to retrive opps created between 01-01-2011 and 06-30-2011.

 

Select o.CreatedDate, o.Id, o.LastModifiedDate,  from Opportunity o where   o.CreatedDate > '1/1/2011' and o.CreatedDate <  '12/31/2011'order by  o.LastModifiedDate

 since createdate is a datetime i get a error saying createdDate is datetime and should not be enclosed in quotes.

Can someone help on how to get this query working

 

Thanks

Prady

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

Try this :

 

Date myDate = date.newinstance(2011, 1, 1);
Date myDate2 = date.newinstance(2011, 12, 31);
List<Opportunity> oppLst = [Select o.CreatedDate, o.Id, o.LastModifiedDate  from Opportunity o where   o.CreatedDate >: myDate and o.CreatedDate <:  myDate2 order by  o.LastModifiedDate] ;

 

Thanks

Ankit Arora

 Blog | Facebook | Blog Page

All Answers

Ankit AroraAnkit Arora

Try this :

 

Date myDate = date.newinstance(2011, 1, 1);
Date myDate2 = date.newinstance(2011, 12, 31);
List<Opportunity> oppLst = [Select o.CreatedDate, o.Id, o.LastModifiedDate  from Opportunity o where   o.CreatedDate >: myDate and o.CreatedDate <:  myDate2 order by  o.LastModifiedDate] ;

 

Thanks

Ankit Arora

 Blog | Facebook | Blog Page

This was selected as the best answer
pradyprady

Thanks Ankit. I kinda figured it out myself

I was looking to run it on the apex explorer rather than on the apex code(which i didnt mention in my question)

 

Select o.CreatedDate, o.Id, o.LastModifiedDate from Opportunity o where   o.CreatedDate > 2011-01-01T00:00:00Z and o.CreatedDate < 2011-12-31T00:00:00Z 

 

MeenakshmiMeenakshmi

Thanks Ankit.

 

Prady,

Your code works fine when field type is 'datetime'. What if the datatype is 'date' ? I am stuck at it in apex explorer.

 

prady-cmprady-cm
Select o.CreatedDate, o.Id, o.LastModifiedDate from Opportunity o where   o.datefield__c > 2011-01-01 and o.datefield__c < 2011-12-31 

 

you remove the time value, just have the date if you are using a date field

 it should work..

MeenakshmiMeenakshmi

Thanks Prady. Great help.

Ankit Kumar 5695Ankit Kumar 5695
SELECT COUNT(Initial_Disbursal_Amount_Available__c) from Opportunity where Disbursement_SMS_Sent > 2023-01-01T00:00:00Z and Disbursement_SMS_Sent < 2023-12-31T00:00:00Z 

Please help