SOQL query WHERE Date = 30_days_ago?
Asked Answered
G

7

8

How do I make a SOQL query like this?

SELECT id FROM Account WHERE LastActivityDate = 30_DAYS_AGO

This produces an error:

MALFORMED_QUERY: 
Account WHERE LastActivityDate = 30_DAYS_AGO
                      ^
Gilstrap answered 8/3, 2011 at 22:9 Comment(3)
Are you executing this query in a web service call or in Apex code? superfell's answer is correct if this is in Apex.Manufactory
Via a webservice, but I just declared the date in the program and passed it as a string to the query as a specific date.Gilstrap
I assumed you were using apex, as you tagged the question with apex.Elfriedeelfstan
E
16

As you're doing this from apex, you can calculate the date in apex, then bind that into your query, e.g.

date d = system.today().addDays(-30);
Account [] acc=  [select id from account where createdDate = :d];
Elfriedeelfstan answered 8/3, 2011 at 23:40 Comment(1)
LAST_N_DAYS would be a better approach since SOQL already has a syntax to do this, or maybe they added it later?Rupe
A
18
SELECT id FROM Account WHERE LastActivityDate = LAST_N_DAYS:30
Aggappora answered 28/3, 2011 at 21:11 Comment(1)
This would be inclusive of all 30 days, OP only wanted the last activity from one day.Histogen
E
16

As you're doing this from apex, you can calculate the date in apex, then bind that into your query, e.g.

date d = system.today().addDays(-30);
Account [] acc=  [select id from account where createdDate = :d];
Elfriedeelfstan answered 8/3, 2011 at 23:40 Comment(1)
LAST_N_DAYS would be a better approach since SOQL already has a syntax to do this, or maybe they added it later?Rupe
B
9
Select Id from Account Where LastActivityDate = N_DAYS_AGO:30
Boyett answered 11/7, 2012 at 18:51 Comment(0)
A
1

LAST_WEEK and LAST_MONTH are also easy and work well.

SELECT id FROM Account WHERE LastActivityDate > LAST_MONTH

For more data look at this link: http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select_dateformats.htm

Androsterone answered 4/4, 2015 at 13:50 Comment(0)
D
0

The page of SOQL date functions appears to have moved here: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

To clarify, SOQL allows a date field (e.g. LastActivityDate) to be compared against a range of dates using a comparison operator. So "LastActivityDate = LAST_MONTH" is equivalent to saying that the date is greater than or equal to the start of the first day of the previous month AND less than or equal to the end of the last day.

Distressed answered 17/9, 2015 at 2:37 Comment(0)
R
0

Since it is 30 days ago, you an use this -

SELECT ID FROM Account WHERE LastActivityDate < LAST_N_DAYS:30
Rupe answered 4/10, 2018 at 16:6 Comment(2)
I don't think LAST_N_DAYS works with the '<' operator. I think it only works with =Roney
We are using it with '<' operator currently. getblogname.wordpress.com/2018/10/04/soql-last_n_days-syntaxRupe
P
0

Your query time period lies in the Date literals provided SFDC its best to use it as the time specified is a broad number , , you just need to provide the no of days and accordingly use the operator which is '=' ,'>' or '<'

LAST_N_DAYS:n       LAST_N_WEEKS:n      LAST_N_MONTHS:n     LAST_N_YEAR:n

NEXT_N_DAYS:n       NEXT_N_WEEKS:n      NEXT_N_MONTHS:n     NEXT_N_YEAR:n

your query would look simpler if you just provided the no of days / month which it falls in .

SELECT id FROM Account WHERE LastActivityDate = LAST_N_MONTHS:1

or

SELECT id FROM Account WHERE LastActivityDate = LAST_N_DAYS:30

Thanks, OQ.

Purism answered 22/9, 2021 at 7:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.