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

soql query

While comparing dates iam getting an error in SOQL query




error is Invalid bind expression comparing datetime and date 


sa cany anyone help me on this error

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Vari,


I think you are comparing datetime field (Created date) with date field (firstrrespondeddate).


So, use following soql query:

select id, campaignid, FirstRespondedDate, status,hasresponded,Campaign.StartDate,Campaign.Type from CampaignMember where ContactId=:opportunity.Principal_Owner__c and hasresponded=true and FirstRespondeddate <: Date.valueOf(opportunity.createdDate) and FirstRespondedDate>:Opportunity.closeDate




Createddate is a datetime field. You need to convert it to Date before it can be compared against a date field.


DateTime dt = opportunity.createdDate;
Date opdate = date.newinstance(dt.year(), dt.month(),;


Now in the query, use "First Responded date <: opdate"    instead of  "FirstRespondeddate <: opportunity.createdDate"


That should solve your problem.


Hi Vari,


Make sure that you compare between (Date with date) or (DateTime with DateTime). If you compare Date with DateTime, then you can convert that DateTime field to Date as Date.valueOf(fieldName) before query and then use the value in query for comparison.


If this post solves your problem, kindly mark it as solution.