Search code examples
salesforcesoqlapex-data-loader

Syntax for SOQL Query - Dynamic Date


Very new to SOQL. Looking to write a query (to be used in Apex Data Loader) that pulls records with a CreatedDate <= 14 days ago. None of the predefined date options (LAST_N_DAYS, etc.) seem to cover what I'm looking for. I'm guessing/hoping there is something similar to DATEADD(D, -14, DATE()) that can dynamically calculate 14 days ago, so that the criteria would ultimately look like CreatedDate <= DATEADD(D, -14, DATE()).

Thanks in advance!


Solution

  • There isn't any Date Add. However looking at your criteria the LAST_N_DAYS builtin should do the trick for you

    Lets say I want to select Data that is older than 14 days ago (including that day) I would do Select Id, CreatedDate from Account where CreatedDate <= LAST_N_DAYS:14 if I need the opposite ie data created in the last 14 days Select Id, CreatedDate from Account where CreatedDate >= LAST_N_DAYS:14