[SalesForce] SOQL query filter WHERE by month

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?

Thanks

Best Answer

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];