How to find out a record was created a particular day of the week in soql in Salesforce?
Use WHERE DAY_IN_WEEK(CreatedDate) = 2 // 2 is Monday, 7 is Saturday
DAY_IN_WEEK is a date function in SOQL, but it doesn't exist in Apex.
1 represents Sunday, and 7 represents Saturday.
Example:
public static List getRecordsCreatedFriday(Integer year, Integer month) { return [ SELECT ... FROM MyObject__c WHERE CALENDAR_YEAR(CreatedDate) = :year AND CALENDAR_MONTH(CreatedDate) = :month AND DAY_IN_WEEK(CreatedDate) = 6 ]; }
Use WeekDay() in formulas https://blog.bessereau.eu/assets/pdfs/formula_date_time_tipsheet.pdf In Apex, use something like this: Date d = System.today(); Datetime dt = (DateTime)d; String dayOfWeek = dt.format('EEEE'); //This returns - Monday, Tuesday, etc.. EEE returns Fri, Sat, etc.
Great solution to the this! Going to be handy knowing 1st Jan 1900 is a Monday! – Matt Lacey · This is great! Since Apex has Date and DateTime as ...To find the day of the week from 192.168.l.254 a Date value, use a known Sunday, for example, January 7, 1900, and subtract it from the date, for example, TODAY() , to get the difference in days.Use WHERE DAY_IN_WEEK(CreatedDate) = 2 // 2 is Monday, 7 is Saturday. DAY_IN_WEEK is a date function in SOQL, but it doesn't exist in Apex.The Weekday() function returns a numeric value for the day of the week from 1 (Sunday) to 7 (Saturday), so the first line gets the weekday for January 1st of the current year. Based on the numeric result, it then adds, subtracts, or does nothing to the date, based on what day it is