I have a custom object having a Date field Expense__Date__c

I need to perform a soql query on the custom object where Expense__Date__c = THIS_MONTH

So my query looks like

List<CustomObj__c> list = [select Name from CustomObj__c where Expense__Date__c = THIS_MONTH];

But it gives an error Unexpected token : THIS_MONTH

When I try to do something like this

List<CustomObj__c> list = [select Name from CustomObj__c where Expense__Date__c.month() = :Date.Today().Month()];

It gives in error unexpected token ')'

How do I perform this filter - where the month for that date field is current month, in a soql query?


  • 1
    Odd. Your query looks correct. For example this query works just fine: [Select ID From Opportunity where CloseDate = THIS_MONTH]. Are you trying to build this query dynamically? If so then THIS_MONTH will not work. – Eric Aug 16 '14 at 17:36
  • No I am not building it dynamically. It is as it is given above. I too find no fault in it but dont know why it wont work – codeinprogress Aug 16 '14 at 17:51

Confirming @Eric, your query

List<CustomObj__c> list = [SELECT Name 
                           FROM CustomObj__c 
                           WHERE Expense__Date__c = THIS_MONTH];

is validated by the following:

I tried out a version of your query myself, and it worked fine.

Though this is besides the point (the Date Literals work), there is always the option of "rolling your own" by something like the following:

Date startOfMonth = Date.today().toStartOfMonth();
Date startOfNextMonth = startOfMonth.addMonths(1);
List<CustomObj__c> list = [SELECT Name 
                           FROM CustomObj__c 
                           WHERE Expense__Date__c >= :startOfMonth
                           AND Expense_Date__c < :startOfNextMonth];
| improve this answer | |
  • 1
    So I just found out that I am only facing this issue when I am working in Eclipse. I saved the class through Salesforce apex class editor and it saved but when I work on it through Eclipse it fails to save. Any reason why this happens? – codeinprogress Aug 16 '14 at 17:59
  • 1
    It seems to be an issue with the Plugin. developer.salesforce.com/forums?id=906F000000094FZIY – Scott Pelak Aug 16 '14 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.