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
RDSSRDSS 

Add 5 days to today date in SOQL query

Hi All,

I want to add 5 days to today's date.
Example : Suppose i have a date field Collection_date__c which is having a date of 24th may 2017 and I am quering it today(19th May 2017). It should display this 24th Mau record.

Select Id, Collection_date__c from Custom_Object__c where Collection_date__c = today + 5;

Please Help

Thanks 
RD
Deepak Maheshwari 7Deepak Maheshwari 7

Hi,

Please use Today() + 5

Maharajan CMaharajan C
Hi,

Try like below:

Date.Today().addDays(5)

Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj
Nithesh NNithesh N
Hi, I believe this one works for you. 
SELECT Id, Collection_date__c FROM Custom_Object__c Where Collection_date__c = NEXT_N_DAYS:5
You can check more date Literals at this Link https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm


 
v varaprasadv varaprasad
Hi ,

Please try below code once.
Date Dat5 = date.today().adddays(5);
system.debug('===Dat5===='+Dat5);

list<contact> lstCons = [select id,name,Birthdate from contact where 
                        Birthdate =: Dat5];
system.debug('==lstCons==='+lstCons);

Thanks
Varaprasad
Ankit Maheshwari 2Ankit Maheshwari 2
Hi,

Could you please use the below code snipt

Date dVar=System.Today()+5;
Select Id, Collection_date__c from Custom_Object__c where Collection_date__c =: dVar;

I used this and it is working properly. If it will not work so please let me know.

Thanks
Ankit Maheshwari
RDSSRDSS
Hi Ankit,

I needs to be done in query only. No APEX.
Is it possible to do this SOQL query only?

Thanks
Ankit Maheshwari 2Ankit Maheshwari 2

Hi,

I am so sorry but in query we can not use this. we need to store date in a variable first then we can use it in query.

you can check the below link for the same. Our concern is about 5 days so here is only the way which i shared with you, rest all you can check on the below link
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

Thanks
Ankit Maheshwari

Nithesh NNithesh N
@RDSS

Use 
WHERE Collection_date__c = NEXT_N_DAYS:5

in Where Clause. Its equivalent to today() + 5 and you dont need any apex too. Everything is done in SOQL.
v varaprasadv varaprasad
SOQL to get next 7 days records
Next_N_Days is used to get next N days records from the object.

The below query will fetch accounts where it's Next Billing Date is today and within next 7 days.

Sample SOQL:

SELECT Id FROM Account WHERE Next_Billing_Date__c >= TODAY AND Next_Billing_Date__c  <= Next_N_Days:7
Kyle NovelliKyle Novelli
Hey RD,

I had a similar problem and found the solution that worked for me.

Sample SOQL query, given your case:
Select Id, Collection_date__c from Custom_Object__c where Collection_date__c = NEXT_N_DAYS:5 AND Collection_date__c > NEXT_N_DAYS:4

I know this thread is pretty old, but hopefully this helps others down the road. 
Priyananth RPriyananth R
Hey,
Salesforce provided a date literal NEXT_N_DAYS:n you want set query as dynamically. try this 
n is integer,starts 00:00:00 of the current day and continues for the next n days

Select Id, Collection_date__c from Custom_Object__c where Collection_date__c = NEXT_N_DAYS:5;
Suraj Tripathi 47Suraj Tripathi 47
Hi RDSS,

You need to modify your query
Select Id, Collection_date__c from Custom_Object__c where Collection_date__c = Date.today().addDays(5);
  OR
Select Id, Collection_date__c from Custom_Object__c where Collection_date__c =  NEXT_N_DAYS:5

Go through these links for more info.
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm

If you find your Solution then mark this as the best answer. 


Thank you!

Regards 
Suraj Tripathi