Question

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?

Was it helpful?

Solution

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

OTHER TIPS

Try following

select date_add(curdate(), interval
case weekday(curdate())
   when 0 then 5
   when 6 then -2
   else -1
end 
day);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top