Selecting data using Dates:
Here is how we can select employees born in March.
select e_id, birth_date
from employee_per
where MONTH(birth_date) = 3;
+------+------------+
| e_id | birth_date |
+------+------------+
| 1 | 1972-03-16 |
| 16 | 1964-03-06 |
+------+------------+
2 rows in set (0.00 sec)
Alternatively, we can use month names instead of numbers.
select e_id, birth_date
from employee_per
where MONTHNAME(birth_date) = 'January';
+------+------------+
| e_id | birth_date |
+------+------------+
| 8 | 1975-01-12 |
| 19 | 1973-01-20 |
| 20 | 1968-01-25 |
+------+------------+
3 rows in set (0.00 sec)
Be careful when using month names as they are case sensitive. Thus, January will work but JANUARY will not!
Similarly, you can select employees born in a specific year or under specific dates.
select e_id, birth_date
from employee_per
where year(birth_date) = 1972;
+------+------------+
| e_id | birth_date |
+------+------------+
| 1 | 1972-03-16 |
| 4 | 1972-08-09 |
+------+------------+
2 rows in set (0.00 sec)
select e_id, birth_date
from employee_per
where DAYOFMONTH(birth_date) = 20;
+------+------------+
| e_id | birth_date |
+------+------------+
| 7 | 1966-08-20 |
| 19 | 1973-01-20 |
+------+------------+
2 rows in set (0.00 sec)
select e_id, birth_date
from employee_per where
MONTH(birth_date) = MONTH(CURRENT_DATE);
+------+------------+
| e_id | birth_date |
+------+------------+
| 8 | 1975-01-12 |
| 19 | 1973-01-20 |
| 20 | 1968-01-25 |
+------+------------+
3 rows in set (0.00 sec)