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)