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

Filter soql queries based on dates

I am trying to write a soql query to return objects based on a datetime field on the object. When I try to use any of the comparison operators on the datetimes ( <, = ) I get the error "No viable alternative at character ' ' " which is ever so helpful in usual salesforce style. The error points at the comparison operator. How can I compare dates in a soql query?
Message Edited by Praetorian65 on 10-01-2009 03:38 AM

When writing a SOQL query, the Date/Time field has to be in the format:



Note that the time is based on GMT, so you'll have to do the conversion.

For example, say you want to return Leads whose last modified date is greater than 9/1/2009 at 10:00AM EST, You would use the following:

Select Id, FirstName, LastName from Lead where LastModifiedDate > 2009-09-01T14:00:00.000Z

(Assuming I got the conversion correct) :smileyvery-happy:


this helped me ./.../thank u so much

sachin sharmasachin sharma

Hi All


I am experiencing a similar kind of problem. I have a date field coming from VF page and I am preparing a query with where clause as:


select testfield from testobject where testfield=:myDateField


I am getting exception in debug logs as:


no viable alternative at character ' '


I looked into DateTime class as well but didn't find any resolution to this. Please suggest.






sachin sharmasachin sharma

Further to this:


My debug logs are showing date as: 1765-09-09 00:00:00 ('T' and 'Z') characters missing. May be this is causing problem.


Any suggestions.




sravani adirajusravani adiraju
happy to see the same issue.. sachin can you plz help out if you have overcome this.... although passing as string helped but it seems im not able to schedule while this is in my batch job as users are of varied locale times & yeah im stuck..plz help out