+ Start a Discussion
jojoforcejojoforce 

How to SOQL Query that has startdate and enddate

We have a custom object MyEvent__c with the following datetime fields StartDateTime__c and EndDateTime__c.

I am trying to get all of the current events and upcoming events.
 
For example, today is 9/14/2020   12:23 PM
 
Start Date/TimeEnd Date/TimeResults
9/14/20 1:00 PM9/14/20 2:00 PMInclude
9/14/20 9:00 AM9/14/20 1:00 PMInclude
9/14/20 8:00 AM9/14/20 8:30 AMDo Not Display
9/14/20 1:00 PM9/16/20 7:00 PMInclude
9/14/20 9:00 AM9/16/20 7:00 PMInclude
9/16/20 9:00 AM9/16/20 7:00 PMDo Not Display


Here is my query so far, but its not returning the correct results.
SELECT Id, Name FROM MyEvent__c WHERE StartDate__c = TODAY OR (StartDate__c >=TODAY AND EndDate__c <= TODAY)



 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Jojo,

As you have stated >= this would mean that in case if the start date is greater or equal the expression would evaluate to true so I think there is no need for StartDate__c = TODAY.

Can you mention if it is working as in is it returning records or is it showing any error?

>> link for quick reference regarding the date comparison in soql query: https://www.wipfli.com/insights/articles/tc-date-comparisons-in-dynamic-soql-queries#:~:text=%3E%20resources%20%3D%20Database.-,query('Select%20Id%2C%20Name%20FROM%20Resource__c%20WHERE,today()%3B

Looking forward for your response.

Regards,
Anutej
Abhishek BansalAbhishek Bansal
SELECT Id, Name FROM MyEvent__c WHERE StartDate__c >=TODAY AND EndDate__c <= TODAY
jojoforcejojoforce
Unfortunately, that queries did not work... I tried the following but I am able to get the first six scenarios correctly, but i also do not want the 7th record. 
 
SELECT Id,StartDate,EndDate
FROM MyEvent__c
WHERE (StartDate >=TODAY OR EndDate >= TODAY)

ScenarioStart DateEnd DateExpected Behavior
Past2020-09-16 9:00 AM2020-09-16 9:30 AMNo
In Progress2020-09-16 9:30 AM2020-09-16 10:30 AMYes
Upcoming2020-09-16 1:00 PM2020-09-16 2:00 PMYes
Multiday Past2020-09-13 1:00 PM2020-09-15 1:00 PMNo
Multiday In Progress2020-09-16 9:00 AM2020-09-18 1:00 PMYes
Multiday Upcoming2020-09-16 1:00 PM2020-09-18 1:00 PMYes
Multiday Future2020-09-20 1:00 PM2020-09-20 4:00 PMNo