
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Get Nth weekday of the month from a column with DATE records in MySQL
We need to find the weekday i.e. week 1 from date 1 to 7, week 2 from date 8 to 14, etc. To get the day, use DAY() function in MySQL. Set the conditions to get the weekday (number) using CASE statement.
Let us now see an example and create a table −
mysql> create table DemoTable ( AdmissionDate date ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-09-12'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('2019-09-06'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-09-26'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('2019-09-30'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+---------------+ | AdmissionDate | +---------------+ | 2019-09-12 | | 2019-09-06 | | 2019-09-26 | | 2019-09-30 | +---------------+ 4 rows in set (0.00 sec)
Following is the query to get the weekday (number) of the month −
mysql> select ( CASE WHEN DAY(AdmissionDate) BETWEEN 1 AND 7 THEN 1 WHEN DAY(AdmissionDate) BETWEEN 8 AND 14 THEN 2 WHEN DAY(AdmissionDate) BETWEEN 15 AND 21 THEN 3 WHEN DAY(AdmissionDate) BETWEEN 22 AND 28 THEN 4 else 5 end ) as NthWeekDayOfMonth from DemoTable;
This will produce the following output −
+-------------------+ | NthWeekDayOfMonth | +-------------------+ | 2 | | 1 | | 4 | | 5 | +-------------------+ 4 rows in set (0.01 sec)
- Related Articles
- Display month names and year from a column with date records with MySQL
- MySQL query to subtract date records with week day and display the weekday with records
- Display distinct dates in MySQL from a column with date records
- How to compare the first date and the last date from a MySQL column with date records?
- Display records from the current date till rest of the same month in MySQL?
- Fetch date records comparing with the current date’s day and month in MySQL
- How to subtract seconds from all the date records in a MySQL column?
- How to display first day and last day of the month from date records in MySQL?
- MySQL query to fetch the latest date from a table with date records
- Comparison of varchar date records from the current date in MySQL
- MySQL query to display the count of distinct records from a column with duplicate records
- Get the first and last date of next month in MySQL?
- How to find last date from records with date values in MySQL?
- Get Month Name from Month number in MySQL?
- Get all the records with two different values in another column with MySQL

Advertisements