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
FCommFComm 

Date comparison in Salesforce

I have 2 date fields

- Start Date

- End Date

 

I want to extract data which End Date is earlier than Start Date, I try to use 'End Date < Start Date' but it return error on soql.

 

Would like to ask how to do date comparison in Salesforce, thank you.

wesnoltewesnolte

Hey

 

What types are teh fields? Is one a datetime and the other a date? Could you post the error message too please.

 

Cheers,

Wes 

FCommFComm

I use data loader to extract data, start date & end date data type = datetime

 

Query:

 

Select Id, Subject, start_date__c, end_date__c From Task where end_date__c < start_date__c

 

Error = unexpected token on 'start_date__c'

wesnoltewesnolte

Hey

 

Doesn't seem this is possible:( There is a workaround though: http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=10285

 

Cheers,

Wes

FCommFComm

Is is possible to do date comparison in View / Report criteria?

 

So that can return result which End date < Start date?

 

Thank you.

FCommFComm
Thank you.
wesnoltewesnolte

In the link in my previous post it suggests creating a formula field on the object that does a comparison and returns a true or false value. The details are on that page.

 

(Quite a silly restriction don't you think)

 

Wes

Prakash@SFDCPrakash@SFDC

Hi,

 

I am also facing the similar problem .

 

I have one VF page . In that page two Input Date fileds (fromDate,toDate) are avialable. If the user choose those fileds i should query the Oppurtunity object and check any records having the CloseDate > fromDate and CloseDate <= todate.

CloseDate is the filed on Opportunity.

 

I am using this query (latter part of the query). here filters is String . Later i am passing this to Database.query(filters);

 

if(fromDate!=null && toDate==null)
                     filters+=' and CloseDate>'+ fromDate ;            
                         //filters+=' and CloseDate >\''+fromDate+'\'';
                
 if(fromDate!=null && toDate!=null)
                     filters+=' and CloseDate >=\''+fromDate+'\'and CloseDate <=\''+toDate+'\'';
                    
  if(fromDate==null && toDate!=null)
                     filters+=' and CloseDate <=\''+toDate+'\'';  

 

This giving an error ----"no viable alternative at character ' ' "

 

Please help me how to resolve this .

 

Many Thanks in Advance

Prakash.N

 

 

Prakash@SFDCPrakash@SFDC

Hi ,

 

I found the solution .

 

we should write the above query   as

if(fromDate!=null && toDate==null)
            {
                    system.debug('###########fromDate:'+fromDate);
                    system.debug('###########filters:'+filters);
                    filters+=' and CloseDate >=:fromDate' ;             
            }            
                 
            if(fromDate!=null && toDate!=null)
                    filters+=' and CloseDate >:fromDate and CloseDate <=:toDate';
                    
            if(fromDate==null && toDate!=null)
                    filters+=' and CloseDate <=:toDate'; 



 

 



Prakash