Lesson

MySQL DAYOFWEEK() Function

10 min reading
Free Course

MySQL DAYOFWEEK(): Get the ODBC Weekday Index (1 = Sunday)

The DAYOFWEEK() function returns the weekday index for a date following the ODBC standard: 1 = Sunday, 2 = Monday, 3 = Tuesday, 4 = Wednesday, 5 = Thursday, 6 = Friday, and 7 = Saturday.

Scheduling and logistics queries use DAYOFWEEK() to separate weekend orders from Monday-through-Friday business days.

Syntax

Pass a single date or datetime value:

DAYOFWEEK(date_value)
  • date_value: A DATE, DATETIME, TIMESTAMP, or valid date string.

Unlike WEEKDAY() (which starts at 0 = Monday), DAYOFWEEK() is always 1-based and begins on Sunday.

Practical Example

Suppose your operations team inspects demo_orders to flag orders placed over the weekend (1 = Sunday or 7 = Saturday) for Monday morning dispatch.

SELECT
    order_id,
    order_date,
    DAYNAME(order_date) AS day_label,
    DAYOFWEEK(order_date) AS odbc_day_index
FROM demo_orders
WHERE DAYOFWEEK(order_date) IN (1, 7);
order_id order_date day_label odbc_day_index
108 2026-05-09 Saturday 7
109 2026-05-10 Sunday 1

Checking IN (1, 7) reliably captures Saturday and Sunday regardless of your server's locale or lc_time_names setting.

Common Mistakes & NULL Behavior

Avoid these frequent errors with DAYOFWEEK():

  • Assuming 1 means Monday: DAYOFWEEK() follows ODBC rules where 1 is Sunday and 2 is Monday. If you expect 0 = Monday through 6 = Sunday, use WEEKDAY() instead.
  • NULL or invalid dates return NULL: Passing NULL, '2026-02-30', or '0000-00-00' produces NULL.
  • Range comparisons for weekdays: Writing DAYOFWEEK(order_date) <= 5 includes Sunday (1) through Thursday (5), missing Friday (6)! Use BETWEEN 2 AND 6 for Monday through Friday.
  • Confusing with WEEK(date, mode): Changing the default_week_format server variable does not affect DAYOFWEEK(). It always uses 1 = Sunday..7 = Saturday.

Frequently Asked Questions

What is the difference between DAYOFWEEK() and WEEKDAY()?

DAYOFWEEK() returns 1..7 with 1 = Sunday and 7 = Saturday. WEEKDAY() returns 0..6 with 0 = Monday and 6 = Sunday.

How do I filter Monday through Friday with DAYOFWEEK()?

Use WHERE DAYOFWEEK(order_date) BETWEEN 2 AND 6, because 2 is Monday and 6 is Friday.

Does DAYOFWEEK() change if my session locale changes?

No. DAYOFWEEK() always returns the fixed integer 1 (Sunday) through 7 (Saturday) in every locale.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum