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
djaindjain 

Compare year in where clause in SOQL

Hi all,
 
In my application i require to fetch data for the year entered by user using SOQL query,
In SOQL i tried the below code:
Code:
date sampleDate=Date.today();
//assume user have entered 2003
Integer diff=sampleDate.year()-2003;
Contact_log__c[] contactLog=[Select a.Id from Contact_log__c a where  a.Last_Import_ModifiedDate__c =(THIS_YEAR-:diff) order by a.Sequence_Number__c desc limit 1];

 

but i get syntax error , Please guide me.

Thanks in advance


 
jeffdonthemicjeffdonthemic
See if this thread helps you...

http://community.salesforce.com/sforce/board/message?board.id=general_development&message.id=11642

Jeff Douglas
Informa Plc
http://blog.jeffdouglas.com
andresperezandresperez

Hi,

You could try something like this:

Code:
DateTime minDateTime = DateTime.newInstance(2003,01,01);
DateTime maxDateTime = DateTime.newInstance(2004,01,01);
Contact_log__c[] contactLog =
[SELECT a.Id
FROM Contact_log__c a
WHERE (a.Last_Import_ModifiedDate__c >= :minDateTime)
AND (a.Last_Import_ModifiedDate__c <= :maxDateTime)
ORDER BY a.Sequence_Number__c DESC
LIMIT 1];
kreshnorkreshnor

You can actually use:

 

where  CALENDAR_YEAR(a.Last_Import_ModifiedDate__c) = (THIS_YEAR-:diff) 

Deepak Venkatesh 6Deepak Venkatesh 6
Hi All,
I am new to Salesforce, I am accessing sales force data trough another tool and writing a select query to pull out data for last two years but I am getting error and below is the query:

select  
Account.Name,
RecordType.Name,
Opportunity.Effective_Date__c
from 
Opportunity where 
CALENDAR_YEAR(Opportunity.Effective_Date__c)>=:(THIS_YEAR -:2) 

Please suggest, at any point of time, I need to pull 2 years data starting from Jan 1st like if am in November 2017, I need to pull data from Jan 2015. 

I get syntax error "Bind Variables Allowed in Apex Code" 
t j 5t j 5
Hi,

You can't compare the result of a date function with a date literal in a WHERE clause. You can refer this https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_date_functions.htm

​Thanks