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 ?

1 Answer 1

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.

6
  • Note that this approach works using static SOQL too as static SOQL supports the same binding syntax.
    – Keith C
    Commented Nov 21, 2015 at 10:07
  • Yes keith, this approach works with static queries too. Now a days I got addicted to dynamic queries more oftern. :P Commented Nov 21, 2015 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
    Commented Nov 21, 2015 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
    Commented Nov 23, 2015 at 17:39
  • @Amit please post your code. Commented Nov 23, 2015 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .