4

For tests proposes, I need to get the current system date. On Oracle we could use:

SELECT SYSDATE FROM DUAL

On SQL SERVER, we can use:

SELECT GETDATE() AS CurrentDateTime

How to query the system date time using SOQL?

Thanks.

  • Question title does not match accepted answer. I want to know the SOQL way too, not the APEX. – Ray Cheng Jun 18 '19 at 18:25
  • Hi @RayCheng, I just posted another answer that might give you another option. – Matheus Goncalves Jun 19 '19 at 19:18
9

You don't need to use SOQL. You can just create a new DateTime and set it to the current date and time like below

DateTime myDateTime = system.now();

Or if you only want the date, then

Date myDate = system.today();
| improve this answer | |
  • 5
    More specifically, SOQL is not SQL. You can't create tables, add indexes, update rows, ad hoc functions, nothing. It's strictly for getting data. I wish the documentation would come right out and say that. – sfdcfox Feb 10 '15 at 18:46
  • @sfdcfox is there a difference in using like: DAY_ONLY(LastModifiedDate) = :System.today() vs DAY_ONLY(LastModifiedDate) = TODAY? and which one is preferred? BTW I'm a big fan of your work here in SFDC SE I've sent you a LinkedIn connection request, will be honored if you let me in your circle of connections, Thanks! – Bahman.A 15 hours ago
  • 1
    @Bahman.A The preferred form would be LastModifiedDate = TODAY; unlike SQL, the date literals actually represent a period of time (e.g. midnight to midnight of the current date, adjusted for the user's time zone). All three forms should result in the same filter being applied. – sfdcfox 15 hours ago
  • Is there a reason why those date literals don't work in test classes? I had to replace them with the system.today() in order for my assertion to pass. ex; [select id from contact where DAY_ONLY (LastModifiedDate) = TODAY]; VS [select id from contact where DAY_ONLY (LastModifiedDate) = :System.today()]; First one didn't return any records, while second one did. This is confusing me, Thanks for your help – Bahman.A 15 hours ago
  • 1
    @Bahman.A Would need to see your code. I don't see why this should make a difference. Perhaps a new question is in order, rather than flooding poor Chris' notification box? – sfdcfox 15 hours ago
5

Alternatively, there is a method System.currentTimeMillis() that returns the current time in milliseconds.

From that, you can create a new DateTime with the DateTime.newInstance(Long) method:

Long longtime = System.currenTimeMillis();
DateTime dt = DateTime.newInstance(longtime);
| improve this answer | |
  • One of those rare times on the SFSE where there are actually two completely different answers. ;) I prefer and use your method. This is just another way. – Peter Knolle Feb 10 '15 at 23:32
  • I use my way more often as well, but I think knowing multiple ways to do things is always helpful. I wasn't aware you could do it this way, love learning new things. Everyday is a school day ;) – Chris Duncombe Feb 10 '15 at 23:39
4

If you cannot use Apex, and need to use SOQL only, then binding is not allowed. Which means you cannot use system.now();

With that in mind, you can use Date Literals, such as YESTERDAY, TODAY, TOMORROW, and so on.

Eg:

SELECT CreatedDate FROM Account WHERE CreatedDate <= TODAY
SELECT CreatedDate FROM Account WHERE CreatedDate <= YESTERDAY
| 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.