+ Start a Discussion
siddarth rajsiddarth raj 

SOQL query to get last 5th day record

Hi

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.

Thnaks
Sid
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,
 http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select_dateformats.htm

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.

Thanks
Sid


 

SIVASASNKARSIVASASNKAR

Hi Siddarth,

Can you try this,

Select id, name from account where createddate=: Date.today().addDays(-5)

For more informatkion click here (http://www.salesforce.com/us/developer/docs/dbcom_apex230/Content/apex_methods_system_date.htm).
siddarth rajsiddarth raj

Hi Siva,

createddate will date and time . hence if we try 
Date.today().addDays(-5) option it works only the record created on 0th hour mean.. let say last 5 th day 3rd may... Result of 
Date.today().addDays(-5) 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 = System.today()-5;

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

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

thanks
Sid