Use the date functions NOW(), DATE(), MONTH(), MONTHNAME(), YEAR(), DAY() and DAYNAME()
Expt. No.34
Page No.68
Date functions
Aim
To display the current date and time, and the day, month, month name, year and day name of each teacher's date of joining.
Concepts used
Queries
USE school;SELECT NOW(), DATE(NOW());SELECT tname, doj, DAY(doj), MONTH(doj), MONTHNAME(doj), YEAR(doj), DAYNAME(doj) FROM teacher;SELECT tname FROM teacher WHERE YEAR(doj) < 2015;
A marked line continues the line above it. In your file, write it on the same line.
Table used
Table: teacher
+-----+------------+---------+--------+------------+ | tid | tname | subject | salary | doj | +-----+------------+---------+--------+------------+ | 1 | Meena Rao | Maths | 72000 | 2012-07-16 | | 2 | Arun Kumar | Physics | 68000 | 2015-04-01 | | 3 | Sana Khan | IP | 75000 | 2010-11-23 | | 4 | Vikas Jain | English | 61000 | 2019-06-10 | +-----+------------+---------+--------+------------+
Output
mysql> USE school;
Database changed
mysql> SELECT NOW(), DATE(NOW());
+---------------------+-------------+
| NOW() | DATE(NOW()) |
+---------------------+-------------+
| 2026-11-21 08:45:08 | 2026-11-21 |
+---------------------+-------------+
1 row in set
mysql> SELECT tname, doj, DAY(doj), MONTH(doj), MONTHNAME(doj), YEAR(doj), DAYNAME(doj) FROM teacher;
+------------+------------+----------+------------+----------------+-----------+--------------+
| tname | doj | DAY(doj) | MONTH(doj) | MONTHNAME(doj) | YEAR(doj) | DAYNAME(doj) |
+------------+------------+----------+------------+----------------+-----------+--------------+
| Meena Rao | 2012-07-16 | 16 | 7 | July | 2012 | Monday |
| Arun Kumar | 2015-04-01 | 1 | 4 | April | 2015 | Wednesday |
| Sana Khan | 2010-11-23 | 23 | 11 | November | 2010 | Tuesday |
| Vikas Jain | 2019-06-10 | 10 | 6 | June | 2019 | Monday |
+------------+------------+----------+------------+----------------+-----------+--------------+
4 rows in set
mysql> SELECT tname FROM teacher WHERE YEAR(doj) < 2015;
+-----------+
| tname |
+-----------+
| Meena Rao |
| Sana Khan |
+-----------+
2 rows in set
✓ Printed by running this query set, not typed by hand.
Result
NOW() gave the date and time when the query was run (yours will differ) and DATE() kept only the date part. For each joining date the functions extracted its parts, e.g. 2012-07-16 is day 16 of July (month 7), 2012, a Monday. Meena Rao and Sana Khan joined before 2015.
Viva voce: what the examiner may ask
01What is the difference between NOW() and DATE(NOW())?
NOW() gives the date and time; DATE() keeps only the date part.
02What does MONTH() return for '2012-07-16'?
7. MONTHNAME() returns "July".
03Can date functions be used in WHERE?
Yes, e.g. WHERE YEAR(doj) < 2015.
Variations for the lab test
- Display teachers who joined in July.
- Display teachers who joined on a Monday.
- Display the year of joining in descending order.
! Common mistake Writing the date in WHERE as 16-07-2012 doesn't match; MySQL dates are 'YYYY-MM-DD'.