Find your content:

Search form

You are here

SQL date constants constants in memory


I am able to run a query with a where clause

Opportunity.CloseDate = LAST_N_DAYS:7

However, I have a custom date attribute for custom object. When I try to set this value in memory:

digest.DM_Digest_date__c = LAST_N_DAYS:7;

I get:

expecting a semi-colon, found ':'

Any ideas?

Attribution to: dublintech

Possible Suggestion/Solution #1

I am able to query on a custom date field okay

Select Id, Name from Opportunity where Lookup__r.Start_Date__c = LAST_N_DAYS:7

Can you double check that it is indeed a Date field. Also, that the relational reference is correct (does it need an __r ?)

Perhaps, it might also be an idea to post your query.

Attribution to: techtrekker

Possible Suggestion/Solution #2

The date constants/literals make sense only in the context of a query. If by "in memory" you mean Apex code, then something like this is needed:

digest.DM_Digest_date__c = - 7;


digest.DM_Digest_date__c = Date.newInstance(2012,12,31);

You can read up more about Date class's methods (plus System class has today() and now() methods too).

Not to mention the fact that LAST_N_DAYS etc all cover timespans, not actual points in time... They're really just a way to quickly write WHERE CreatedDate > :startPoint AND CreatedDate < :endPoint. So you can't really assign this to one date/time variable, you'd need 2 fields if you want to record a date "from-to".

Attribution to: eyescream
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content