9

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() ];

3 Answers 3

10

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() ];    
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.

2
  • Hi Ray, that query should return one record but is not returning anything.
    – Carlos
    May 30, 2014 at 22:22
  • @Carlos The query is in GMT, though. Make sure you're actually on the right date.
    – sfdcfox
    May 30, 2014 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() ];

You must log in to answer this question.

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