Close

2023-10-12

Determining the WeekEnd for MySQL

Determining the WeekEnd for MySQL

The shortest way to determine if a date is a weekend or a weekday in a MySQL server is by using the DAYOFWEEK() function. This function returns a number from 1 to 7, where 1 represents Sunday and 7 represents Saturday.

Here’s how you can use it:

SELECT 
    CASE 
        WHEN DAYOFWEEK(date_column) IN (1, 7) THEN 'Weekend'
        ELSE 'Weekday'
    END AS day_type
FROM table_name;

Replace date_column with the name of your date column and table_name With the name of your table. This SQL query will return ‘Weekend’ for dates that fall on a Saturday or Sunday and ‘Weekday’ for all other dates.