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.

1 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .