🧚 주목! Beekeeper Studio는 빠르고 현대적이며 오픈 소스 데이터베이스 GUI입니다 다운로드
September 9, 2024 작성자: Matthew Rathbone

Identifying Weekdays Using Oracle’s TO_CHAR() Function

The following example shows how to use TO_CHAR() to return only rows where the date falls on a weekday.

SELECT order_id, order_date
FROM orders
WHERE TO_CHAR(order_date, 'DY') NOT IN ('SAT', 'SUN');

Expected Output:

ORDER_ID | ORDER_DATE
---------|------------
1001     | 04-SEP-2023
1002     | 05-SEP-2023
1005     | 06-SEP-2023
1010     | 07-SEP-2023

In this query:

  • TO_CHAR(order_date, 'DY') extracts the abbreviated day name from the order_date column.
  • The NOT IN ('SAT', 'SUN') clause filters out any rows where the day is Saturday or Sunday.

Working with Full Day Names

If your database is set to a locale where the abbreviated day names differ, or you prefer to work with full day names, you can modify the query as follows:

SELECT employee_id, work_date
FROM employee_schedule
WHERE TO_CHAR(work_date, 'DAY') NOT IN ('SATURDAY', 'SUNDAY');

Expected Output:

EMPLOYEE_ID | WORK_DATE
------------|-----------
200         | 01-SEP-2023
201         | 02-SEP-2023
202         | 03-SEP-2023

Filtering Weekdays Between Two Dates

Often, you may want to retrieve records between two dates but only for weekdays. Here’s how you can extend the logic to do that.

Example Query: Selecting Weekdays Between Two Dates

SELECT invoice_id, invoice_date
FROM invoices
WHERE invoice_date BETWEEN TO_DATE('2023-09-01', 'YYYY-MM-DD') 
                       AND TO_DATE('2023-09-10', 'YYYY-MM-DD')
AND TO_CHAR(invoice_date, 'DY') NOT IN ('SAT', 'SUN');

Expected Output:

INVOICE_ID | INVOICE_DATE
-----------|--------------
3001       | 01-SEP-2023
3005       | 04-SEP-2023
3007       | 05-SEP-2023
3010       | 06-SEP-2023

Handling Different Languages and Locales

Oracle may display day names based on your session’s NLS (National Language Support) settings. For example, if your session is set to a language other than English, the day abbreviations like ‘MON’, ‘TUE’ may be different. You can check the current settings by running:

SELECT * FROM NLS_SESSION_PARAMETERS WHERE PARAMETER = 'NLS_TERRITORY';

If necessary, you can override these settings at the session level to ensure consistent results:

ALTER SESSION SET NLS_TERRITORY = 'AMERICA';

This sets the territory to ‘AMERICA’, ensuring that day names like ‘MON’, ‘TUE’, etc., are used.

Using the NEXT_DAY() Function

Sometimes you may want to calculate the next weekday from a given date. Oracle’s NEXT_DAY() function helps you find the next occurrence of a specific weekday.

Example Query: Finding the Next Weekday

SELECT NEXT_DAY(SYSDATE, 'MONDAY') AS next_monday
FROM dual;

Expected Output:

NEXT_MONDAY
------------
11-SEP-2023

In this case, NEXT_DAY(SYSDATE, 'MONDAY') returns the next ‘Monday’ after the current date.

Conclusion

Filtering weekdays in Oracle SQL is a common requirement in many applications. By using the TO_CHAR() function to extract the day of the week and filtering out weekends, you can work with weekdays only. In more complex scenarios, functions like NEXT_DAY() and date ranges can be combined to create more sophisticated queries.

Other articles you may enjoy:

Beekeeper Studio는 무료 & 오픈 소스 데이터베이스 GUI입니다

제가 사용해 본 최고의 SQL 쿼리 & 편집기 도구입니다. 데이터베이스 관리에 필요한 모든 것을 제공합니다. - ⭐⭐⭐⭐⭐ Mit

Beekeeper Studio는 빠르고 직관적이며 사용하기 쉽습니다. Beekeeper는 많은 데이터베이스를 지원하며 Windows, Mac, Linux에서 훌륭하게 작동합니다.

Beekeeper의 Linux 버전은 100% 완전한 기능을 갖추고 있으며, 기능 타협이 없습니다.

사용자들이 Beekeeper Studio에 대해 말하는 것

★★★★★
"Beekeeper Studio는 제 예전 SQL 워크플로를 완전히 대체했습니다. 빠르고 직관적이며 데이터베이스 작업을 다시 즐겁게 만들어 줍니다."
— Alex K., 데이터베이스 개발자
★★★★★
"많은 데이터베이스 GUI를 사용해 봤지만, Beekeeper는 기능과 단순함 사이의 완벽한 균형을 찾았습니다. 그냥 작동합니다."
— Sarah M., 풀스택 엔지니어

SQL 워크플로를 개선할 준비가 되셨나요?

download 무료 다운로드