I need to get the previous weekday. So when it's monday I need to get the result friday but I can't seem to understand the WEEKDAY() function. Could someone help to start with this?
Asked
Active
Viewed 1,785 times
2 Answers
2
WEEKDAY()
returns 0 to 6 depending on which day of the week it is. You could put logic in the code to ignore weekend conditions, and go back to the Friday.
Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
So if WEEKDAY(date) - 1 == 5 || WEEKDAY(date) - 1 == 6
then make it equal 4 (Friday) instead.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekday

Sunny Patel
- 7,830
- 2
- 31
- 46

Marshall Tigerus
- 3,675
- 10
- 37
- 67
0
Try following
select date_add(curdate(), interval
case weekday(curdate())
when 0 then 5
when 6 then -2
else -1
end
day);

user353gre3
- 2,747
- 4
- 24
- 27