How to search dob in a table, which is in the yyyy-mm-dd structure and compare only with a specific yyyy format (year) in MySQL?


For this, use MySQL YEAR() as in the below syntax −

select * from yourTableName where year(yourColumnName)=’yourYearValue’;

Let us first create a table −

mysql> create table DemoTable1322
-> (
-> DOB date
-> );
Query OK, 0 rows affected (0.55 sec)

Example

Insert some records in the table using insert command −

mysql> insert into DemoTable1322 values('1999-04-12');
Query OK, 1 row affected (0.68 sec)
mysql> insert into DemoTable1322 values('2010-12-01');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1322 values('2015-03-09');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable1322 values('2007-05-24');
Query OK, 1 row affected (0.08 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1322;

Output

+------------+
| DOB        |
+------------+
| 1999-04-12 |
| 2010-12-01 |
| 2015-03-09 |
| 2007-05-24 |
+------------+
4 rows in set (0.00 sec)

Example

Here is the query to search dob which is in the yyyy-mm-dd structure. We’re comparing with yyyy i.e. a specific year −

mysql> select * from DemoTable1322 where year(DOB)='2015';

Output

+------------+
| DOB        |
+------------+
| 2015-03-09 |
+------------+
1 row in set (0.01 sec)

Updated on: 08-Jul-2020

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements