
- 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
MySQL query to subtract date records with week day and display the weekday with records
For this, you can use DATE_FORMAT(). Let us first create a table −
mysql> create table DemoTable1820 ( AdmissionDate varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1820 values('20/10/2019'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1820 values('19/12/2018'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1820 values('16/04/2017'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1820;
This will produce the following output −
+---------------+ | AdmissionDate | +---------------+ | 20/10/2019 | | 19/12/2018 | | 16/04/2017 | +---------------+ 3 rows in set (0.00 sec)
Here is the query to subtract date records with week day and display week day name along with records −
mysql> select DATE_FORMAT(SUBDATE(STR_TO_DATE(AdmissionDate,'%d/%m/%y'), WEEKDAY(STR_TO_DATE(AdmissionDate,'%d/%m/%y'))), '%a %d %b') from DemoTable1820;
This will produce the following output −
+-------------------------------------------------------------------------------------------------------------------------+ | DATE_FORMAT(SUBDATE(STR_TO_DATE(AdmissionDate,'%d/%m/%y'), WEEKDAY(STR_TO_DATE(AdmissionDate,'%d/%m/%y'))), '%a %d %b') | +-------------------------------------------------------------------------------------------------------------------------+ | Mon 19 Oct | | Mon 14 Dec | | Mon 13 Apr | +-------------------------------------------------------------------------------------------------------------------------+ 3 rows in set, 6 warnings (0.00 sec)
- Related Articles
- MySQL query to select records with a particular date?
- MySQL query to select records with a particular date and time?
- MySQL query to display the count of distinct records from a column with duplicate records
- Get Nth weekday of the month from a column with DATE records in MySQL
- MySQL query to fetch the latest date from a table with date records
- How to display first day and last day of the month from date records in MySQL?
- Fetch date records comparing with the current date’s day and month in MySQL
- Display month names and year from a column with date records with MySQL
- MySQL query to return all records with a datetime older than 1 week
- MongoDB query to search date records using only Month and Day
- How to compare Year, Month and Day in a MySQL query and display matching records
- Display distinct dates in MySQL from a column with date records
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?
- How to change the date in a table with date records with MySQL?
- How to display the day name on the basis of Date of Birth records in MySQL?

Advertisements