3

I have a 2 datetime field(Start_Time__c and End_Time__c) and I need to compare them with current time. I am writing the following SOQL Query :

Select id from objTime where Start_Time__c >= SystemTime.now() AND End_Time__c

But it is unable to fetch to the comparison of date time, do we have any solution for resolving the same ?

4

You can use static or dynamic SOQL here. It would be as below

Static soql:

DateTime currentTime = System.now();
List<Account> accounts  = [SELECT Id From Acccount WHERE Start_Time__c >= :currentTime AND End_Time__c <= TODAY ];

Dynamic soql:

DateTime currentTime = System.now();

List<Account> accounts = Database.query('SELECT Id From Acccount WHERE Start_Time__c >= :currentTime AND End_Time__c <= TODAY ');

I tried this on account , you can use this on any sobject. As a common practice if this helps mark this as solution.

|improve this answer|||||
  • Note that this approach works using static SOQL too as static SOQL supports the same binding syntax. – Keith C Nov 21 '15 at 10:07
  • Yes keith, this approach works with static queries too. Now a days I got addicted to dynamic queries more oftern. :P – Kiran Machhewar Nov 21 '15 at 10:09
  • 1
    OK, then surely the answer is "you can bind the current time into a static or dynamic SOQL query"; the choice of static or dynamic is unrelated to the question. – Keith C Nov 21 '15 at 10:12
  • Hi,I am still having issue when I use the compare Start_time__c>= system.now() ; see below for the exception that I am getting USER_DEBUG|[140]|DEBUG| ************************* currentTime = 2015-11-23 17:13:42 11:13:42.616 (616612307)|VF_PAGE_MESSAGE|Exception encountered, please contact administrator. – Amit Nov 23 '15 at 17:39
  • @Amit please post your code. – Kiran Machhewar Nov 23 '15 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.