문제

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?

도움이 되었습니까?

해결책

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

다른 팁

Try following

select date_add(curdate(), interval
case weekday(curdate())
   when 0 then 5
   when 6 then -2
   else -1
end 
day);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top