2

In account record I have a custom myDate__c field of type date.

I need to retrieve all Account records where myDate__c is older than 45 days.

I've tried with the following query:

SELECT id, name 
FROM Account 
WHERE myDate__c <= today() - 45

However I received a warning message:

Date and number are incompatible types

How to change my query to retrieve the correct Account records?

I need only SOQL. No Apex.

7

I recommend you read up on Date Formats and Date Literals in the SOQL and SOSL Reference. This resource covers several ways you can format date fields that you may find useful in the future. Specifically for your current use case:

Date Literal
LAST_N_DAYS:n

Range
For the number n provided, starts 00:00:00 of the current day and continues for the past n days.

Example
SELECT Id FROM Account WHERE CreatedDate = LAST_N_DAYS:365

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.