
- 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 search for a date in MySQL timestamp field?
You can use DATE() function from MySQL for this. Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentAdmissionDate timestamp ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentAdmissionDate) values('2011-01-12 12:34:43'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentAdmissionDate) values('2012-10-23 11:32:21'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentAdmissionDate) values('2001-02-14 05:12:01'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(StudentAdmissionDate) values('2018-12-31 15:10:04'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(StudentAdmissionDate) values('2019-04-16 11:04:10'); Query OK, 1 row affected (0.81 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 −
+-----------+----------------------+ | StudentId | StudentAdmissionDate | +-----------+----------------------+ | 1 | 2011-01-12 12:34:43 | | 2 | 2012-10-23 11:32:21 | | 3 | 2001-02-14 05:12:01 | | 4 | 2018-12-31 15:10:04 | | 5 | 2019-04-16 11:04:10 | +-----------+----------------------+ 5 rows in set (0.00 sec)
Here is the query to search for a date in timestamp field.
mysql> select *from DemoTable where date(StudentAdmissionDate)='2001-02-14';
This will produce the following output −
+-----------+----------------------+ | StudentId | StudentAdmissionDate | +-----------+----------------------+ | 3 | 2001-02-14 05:12:01 | +-----------+----------------------+ 1 row in set (0.04 sec)
We can also search for a date between a given range:
mysql> select *from DemoTable where StudentAdmissionDate >='2012-01-01' and StudentAdmissionDate < '2019-12-01';
This will produce the following output −
+-----------+----------------------+ | StudentId | StudentAdmissionDate | +-----------+----------------------+ | 2 | 2012-10-23 11:32:21 | | 4 | 2018-12-31 15:10:04 | | 5 | 2019-04-16 11:04:10 | +-----------+----------------------+ 3 rows in set (0.00 sec)
- Related Articles
- How to Insert custom date into MySQL timestamp field?
- How to select date from timestamp in MySQL?
- How to update a timestamp field of a MySQL table?
- Fastest way to search for a date from the date records in MySQL
- MySQL database field type for search query?
- How to search a record by date in MySQL table?
- Creating a table with a TIMESTAMP field in MySQL?
- MySQL query to select date from timestamp?
- Select timestamp as date string in MySQL?
- Set current date and time to timestamp in MySQL
- Convert MySQL Unix-Timestamp format to date format?
- Get only the date in timestamp in MySQL?
- Combine date and time column into a timestamp in MySQL?
- Display Timestamp before the current date in MySQL
- Display only date from timestamp value in MySQL

Advertisements