find weekday in soq...
 
Notifications
Clear all

[Solved] find weekday in soql

3 Posts
2 Users
0 Likes
1,098 Views
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

How to find out a record was created a particular day of the week in soql in Salesforce?

Issue Tags
2 Replies
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

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.
Reply

Warning: Trying to access array offset on value of type null in /home3/colbrj8a/public_html/wp-content/plugins/wpforo/includes/functions-template.php on line 193
Posts:
Warning: Trying to access array offset on value of type null in /home3/colbrj8a/public_html/wp-content/plugins/wpforo/themes/2022/layouts/4/post.php on line 27
0

Warning: Trying to access array offset on value of type null in /home3/colbrj8a/public_html/wp-content/plugins/wpforo/themes/2022/layouts/4/post.php on line 43
 Anonymous
Joined:
Warning: Trying to access array offset on value of type null in /home3/colbrj8a/public_html/wp-content/plugins/wpforo/themes/2022/layouts/4/post.php on line 53
55 years ago

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

 
Reply
Share: