Posts

Showing posts with the label DAY_ONLY

Salesforce Fact #719 | DAY_ONLY() to get records within two dates

Suppose we need to fetch a list of records between two dates and also needs to skip for 2 days within the range. The filter field is of datetime type. We can make use of the DAY_ONLY() function for this. In this example, we are fetching account records created between 2023-07-01 and 2023-07-16 and skip the records created on 2023-07-06, 2023-07-07. SOQL:  SELECT Id, Name, CreatedDate  FROM Account WHERE DAY_ONLY(CREATEDDATE) >= 2023-07-01 AND DAY_ONLY(CREATEDDATE) <= 2023-07-16 AND DAY_ONLY(CREATEDDATE) NOT IN (2023-07-06, 2023-07-07)

Salesforce Fact #651 | IN operator with Day_Only()

We can use IN operator to put filter on multiple dates while using Day_Only function. For example, the below SOQL returns the accounts which were created on 27th and 28th Jan of 2023. SELECT Id, Name, CreatedDate FROM Account WHERE Day_Only(CreatedDate) IN (2023-01-27, 2023-01-28) The same works perfectly with bind variables as well: Date d1 = Date.parse('01/27/2023'); Date d2 = Date.parse('01/28/2023'); List<Account> accList = [SELECT Id, Name, CreatedDate FROM Account WHERE Day_Only(CreatedDate) IN (:d1, :d2)];

Salesforce Fact #2 | Date check SOQL

If we want to extract the day from a date/time field, we can use the DAY_ONLY function. Suppose, if I want to get the accounts which were created on 1st Jan,2021 we can use the below query: SELECT Name FROM Account WHERE Day_Only(CreatedDate) = 2021-01-01 i.e. (YYYY-MM-DD) Note: we need not enclose the input date inside quotes.