
- 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
Selecting from a MySQL table based on parts of a timestamp?
Let us first create a table −
mysql> create table DemoTable ( AdmissionDate timestamp ); Query OK, 0 rows affected (0.90 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2007-01-10'); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable values('2015-07-12'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2017-11-01'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('2019-08-29'); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+---------------------+ | AdmissionDate | +---------------------+ | 2007-01-10 00:00:00 | | 2015-07-12 00:00:00 | | 2017-11-01 00:00:00 | | 2019-08-29 00:00:00 | +---------------------+ 4 rows in set (0.00 sec)
Following is the query to select from a MySQL table based on parts of a timestamp. Here, we are selecting on the basis of year −
mysql> select *from DemoTable where year(AdmissionDate)='2017';
This will produce the following output −
+---------------------+ | AdmissionDate | +---------------------+ | 2017-11-01 00:00:00 | +---------------------+ 1 row in set (0.23 sec)
- Related Articles
- Selecting data from a MySQL table based on a specific month?
- Select total from a MySQL table based on month
- How can we create MySQL view by selecting data based on pattern matching from base table?
- Create a MySQL table from already created table selecting specific rows?
- Delete only some rows from a table based on a condition in MySQL
- Selecting and displaying only some rows from a column in a MySQL table
- Update a table based on StudentId value in MySQL?
- How to update a timestamp field of a MySQL table?
- Selecting the top occurring entries in MySQL from a table with duplicate values?
- Creating a table with a TIMESTAMP field in MySQL?
- How can we create a MySQL view by selecting some range of values from a base table?
- How can I export values based on some conditions from MySQL table into a file?
- How can we create a new MySQL table by selecting specific column/s from another existing table?
- How to create a MySQL table based on JDBC Result Set?
- Selecting a value in custom order from another column in a MySQL table with a single query

Advertisements