
- 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
How to return today's records using DATE from DATETIME Field?
Let us first create a table in which one of the column is of datetime type;
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ShippingDate datetime ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command.
mysql> insert into DemoTable(ShippingDate) values('2019-03-01 05:45:32'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(ShippingDate) values('2019-04-13 11:34:56'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ShippingDate) values('2019-03-15 04:45:23'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ShippingDate) values('2019-04-11 12:10:02'); Query OK, 1 row affected (0.17 sec)
Following is the query to display all records from the table using select statement:
mysql> select *from DemoTable;
This will produce the Following Output -
+----+---------------------+ | Id | ShippingDate | +----+---------------------+ | 1 | 2019-03-01 05:45:32 | | 2 | 2019-04-13 11:34:56 | | 3 | 2019-03-15 04:45:23 | | 4 | 2019-04-11 12:10:02 | +----+---------------------+ 4 rows in set (0.00 sec)
Following is the query to get today's record using DATE from DATETIME field. Let’s say today’s date is “2019-04-13”:
mysql> select *from DemoTable where ShippingDate > date_sub(curdate(),interval 1 day);
This will produce the Following Output -
+----+---------------------+ | Id | ShippingDate | +----+---------------------+ | 2 | 2019-04-13 11:34:56 | +----+---------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to subtract date from today's date in JavaScript?
- How to get today's date in Java8?
- Compare DATE string with string from MySQL DATETIME field?
- How to update date of datetime field with MySQL?
- MySQL Select Date Equal to Today and return results for the same date?
- Extracting only date from datetime field in MySQL and assigning it to PHP variable?
- How to get the date between TODAY and TODAY-7”?
- Get the Day of the Week from Today's Date in Java
- MySQL query to select closest date from today?
- How to select only MySQL date from datetime column?
- Add DATE and TIME fields to get DATETIME field in MySQL?
- MySQL Query to convert from datetime to date?
- SELECT MySQL rows where today's date is between two DATE columns?
- How to part DATE and TIME from DATETIME in MySQL?
- How to find last date from records with date values in MySQL?

Advertisements