Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

Does LAST N DAYS in Soql include TODAY?

Asked by David Edmunds Feb 27, 2023 9.1K views 3 answers
Share

About this question

 I would like to ask about the definition of LAST_N_DAYS:n


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


Is
CreatedDate = LAST_N_DAYS:1
equal to
(CreatedDate >= 2017-08-30T00:00:00Z AND CreatedDate <= 2017-08-30T23:59:59Z)
OR CreatedDate  = TODAY
and
CreatedDate >= YESTERDAY
To be more specific, aren't LAST_N_DAYS in fact LAST_N_DAYS + 1 (Today)?

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Feb 4, 2025

In SOQL (Salesforce Object Query Language), the LAST_N_DAYS function is used to filter records from the last N days, but the key question is: Does it include today?

✅ Yes, LAST_N_DAYS Includes Today

  • The LAST_N_DAYS function counts backward from today, meaning today is included in the range.
  • If you query LAST 7 DAYS, it will retrieve records from today and the past 6 days.

Was this helpful?

Ranjana Admin JanBask Expert

Answered on May 29, 2024

In Salesforce Object Query Language (SOQL), the LAST_N_DAYS:n date literal includes today in the range of days. This means that when you use LAST_N_DAYS:n, the query will include records from today and the previous n-1 days.

To illustrate this with an example, let's consider you want to fetch records from the last 7 days including today. The SOQL query would look like this:

SELECT Id, Name, CreatedDate
FROM Account
WHERE CreatedDate = LAST_N_DAYS:7

This query retrieves all Account records where the CreatedDate is within the last 7 days, including today.

Clarification

  • If today is May 29th: LAST_N_DAYS:7 will include records from May 23rd to May 29th, inclusive.
  • If today is May 29th and time is 3 PM: It includes records created from 12:00 AM on May 23rd to the current time on May 29th (3 PM).

Testing with Real Data

To ensure you understand how this works with your data:
Create Test Records: Insert records with known CreatedDate values.
Run Queries: Use LAST_N_DAYS:n in your queries and observe the results to confirm it includes today.

Here’s a more concrete example with comments:

This will return contacts created from May 23rd to May 29th, inclusive.

Additional Date Literals

  • To provide more context, here are other useful date literals in SOQL:
  • YESTERDAY: Includes only yesterday’s records.
  • TODAY: Includes only today’s records.
  • TOMORROW: Includes only tomorrow’s records.
  • LAST_WEEK: Includes all records from last week.
  • THIS_WEEK: Includes all records from this week.
  • NEXT_WEEK: Includes all records from next week.
  • LAST_MONTH: Includes all records from last month.
  • THIS_MONTH: Includes all records from this month.
  • NEXT_MONTH: Includes all records from next month.
  • LAST_N_DAYS:n: Includes the last n days including today.
  • NEXT_N_DAYS:n: Includes the next n days including today.

Conclusion

Yes, LAST_N_DAYS:n in SOQL includes today as part of the n days. This is useful for queries where you want to consider data from the current day and a set number of days preceding it.



Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.