function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
siddarth rajsiddarth raj 

SOQL query to get last 5th day record


Can I be helped in writing an SOQL to find the records which created on last 5 th day. Means If I am querying today i.e 8-5-2015 i expect  the result of a record should be actually created on 03-05-2015.

Arunkumar RArunkumar R
Hi Siddarth,

You can use the below query using LAST_N_DAYS function,

Select id, name from account where createddate=LAST_N_DAYS:5

You can also find more info at,

Please mark this as a solution if you are problem solved.
siddarth rajsiddarth raj

Hi arunkumar,

Thanks for the time. But sorry that doenst work in my case. I wanted to precisely last 5th day not records being created in last 5 days.

LAST_N_DAYS:n  is works for last n days not the last nth day.




Hi Siddarth,

Can you try this,

Select id, name from account where createddate=:

For more informatkion click here (
siddarth rajsiddarth raj

Hi Siva,

createddate will date and time . hence if we try option it works only the record created on 0th hour mean.. let say last 5 th day 3rd may... Result of is 2015-05-03T00:00:00:000Z .... many records will be created during the day. 

Thanks for you all helping me in finding the solution. I found one approach . Here is what i am gonna use

Date t =;

Datetime ed = Datetime.newInstance(t.year(), t.month(),,23,59,59);

Account  a = [select id from account where createddate >= :t and createddate <= :ed];