I have a custom object having a Date field Card__Date__c

I need to perform a soql query on the custom object where Card__Date__c in 2017

So my query looks like

List<TimeCard__c> tcList = [select id, Name from TimeCard__c where Card__Date__c = 2018 ];

This one is wrong. Not sure about where cause.

How do I perform this - where the year for that date field is in a fixed year ?


Substitute the year with the one you want to get records for:

Documentation here

   SELECT CreatedDate, Name
    FROM Account
    WHERE CALENDAR_YEAR(CreatedDate) = 2018
Date startDate = date.newinstance(2017, 1, 1);
Date endDate = date.newinstance(2017, 12, 31);

List<Account> accList = [select id, name from account where CreatedDate >: startDate and CreatedDate <: endDate];

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.