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
JSchneiderJSchneider 

How do I select based on Date is THIS MONTH?

String query = 'SELECT Id,Run_Maintenance_Calculator_del__c FROM Account WHERE PIN_Code__c != null';

I like to add
AND Custom_Date__c = THIS MONTH
but cannot figure it out.  I appreciate the help. 
Best Answer chosen by JSchneider
KaranrajKaranraj
Try the below query
SELECT Id,Run_Maintenance_Calculator_del__c FROM Account WHERE PIN_Code__c != null AND Custom_Date__c < THIS MONTH
Check this link to know more about Date formates and Literals https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
 

All Answers

KaranrajKaranraj
Try the below query
SELECT Id,Run_Maintenance_Calculator_del__c FROM Account WHERE PIN_Code__c != null AND Custom_Date__c < THIS MONTH
Check this link to know more about Date formates and Literals https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
 
This was selected as the best answer
Vivek DeshmaneVivek Deshmane
Hi Jschneider,
Please try below code and let me know.
Integer mnth = System.Today().MOnth();
Integer yr = System.Today().Year();

String query = 'SELECT Id,Run_Maintenance_Calculator_del__c FROM Account WHERE PIN_Code__c != null AND CALENDAR_MONTH(CreatedDate) = mnth ANDCALENDAR_YEAR(CreatedDate) = yr ';

Best Regards,
-Vivek
Vivek DeshmaneVivek Deshmane
Hi Jschenider,
Please try below code and let me know.
Integer mnth = System.Today().MOnth();
Integer yr = System.Today().Year();

String query = 'SELECT Id,Run_Maintenance_Calculator_del__c FROM Account WHERE PIN_Code__c != null AND CALENDAR_MONTH(CreatedDate) ='+ mnth +'and CALENDAR_YEAR(CreatedDate) ='+ yr ;
System.debug('query==>'+query);
List<Account> searchAccounts = Database.query(query);
Best Regards,
-Vivek
JSchneiderJSchneider
+1 Karanraj

I had tried THISMONTH but never thought to put the underscore in the middle.  That link had all the documentation I needed.

Thanks!
JSchneiderJSchneider
Final version:
String query = 'SELECT Id,Run_Maintenance_Calculator_del__c FROM Account WHERE PIN_Code__c != null AND Maintenance_Expiration_Date__c = THIS_MONTH';

 
Amit Chaudhary 8Amit Chaudhary 8
Hi JSchneider,

Please check below link for all date variable 
https://help.salesforce.com/HTViewHelpDoc?id=custom_dates.htm&language=en_US (https://help.salesforce.com/HTViewHelpDoc?id=custom_dates.htm&language=en_US)

Please check below link for date function in salesforce
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_date_functions.htm

Some example for you. I hope that will help you
SELECT id FROM Account WHERE LastActivityDate = LAST_N_DAYS:30
 
SELECT id FROM Account WHERE LastActivityDate > LAST_MONTH

Please more example in below link:-
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

Please try below query:-
SELECT Id,Run_Maintenance_Calculator_del__c FROM Account WHERE PIN_Code__c != null and Custom_Date__c  < THIS_MONTH

Please let us know if this will help you.

Thanks
Amit Chaudhary