8

I was wondering if it is possible to compare a DateTime and Date in SOQL.

I am trying to pull data for the product that where sold today Date.Today().

Purchase_Date__c is a DATETIME field

Date d = Date.today();

hot = [SELECT id,Name,NRProduct__c, QuantityOrdered__c,Purchase_Date__c   
         FROM MWSOrderItem__c   
         WHERE Purchase_Date__c =:d AND Buyer_lookup__c =:UserInfo.getUserId() ];
8

I found the solution, after a lot of research Here's a link!

code:

hot = [SELECT id,Name,NRProduct__c, QuantityOrdered__c,Purchase_Date__c   
          FROM MWSOrderItem__c   
          WHERE DAY_ONLY(Purchase_Date__c )=:Date.today() AND 
                Buyer_lookup__c =:UserInfo.getUserId() ];    
| improve this answer | |
2

Yes, that will work as expected.

You will get all MWSOrderItem__c with Purchase_Date__c between 12:00am and 11:59pm from variable d in our case Today.

| improve this answer | |
  • Hi Ray, that query should return one record but is not returning anything. – Carlos May 30 '14 at 22:22
  • @Carlos The query is in GMT, though. Make sure you're actually on the right date. – sfdcfox May 30 '14 at 22:29
1

I would suggest using TODAY instead. Here is a doc on how to use it and how it works Date Formats and Date Literals

hot = [SELECT id,Name,NRProduct__c, QuantityOrdered__c,Purchase_Date__c   
     FROM MWSOrderItem__c   
     WHERE Purchase_Date__c = TODAY AND Buyer_lookup__c =:UserInfo.getUserId() ];
| improve this answer | |

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.