MySQL Date Functions - Complete Beginner Tutorial
Quick Answer
MySQL date functions allow you to manipulate, format, and extract information from date and time values stored in your database. These functions help perform operations like getting the current date, extracting parts of a date, calculating intervals, and formatting dates for display.
Learning Objectives
- Understand the purpose and usage of MySQL date functions.
- Learn how to extract and manipulate date and time components.
- Apply date functions to solve common database querying tasks.
Introduction
Working with dates and times is a common requirement in database applications. MySQL offers a variety of built-in functions to handle date and time values efficiently.
This tutorial covers the most useful MySQL date functions, providing examples and explanations to help you manipulate date and time data effectively.
Time is what keeps everything from happening at once.
Basic Date Functions
MySQL includes several functions to retrieve the current date and time or extract parts of a date value.
- NOW() returns the current date and time.
- CURDATE() returns the current date.
- CURTIME() returns the current time.
- YEAR(), MONTH(), DAY() extract respective parts from a date.
Getting the Current Date and Time
Use NOW() to get the current date and time in 'YYYY-MM-DD HH:MM:SS' format.
CURDATE() returns only the date part, useful when time is not needed.
Extracting Date Parts
You can extract specific parts of a date using functions like YEAR(), MONTH(), and DAY().
These are helpful for filtering or grouping data by year, month, or day.
Date Arithmetic Functions
MySQL provides functions to add or subtract intervals from dates, enabling date calculations.
- DATE_ADD() adds a specified time interval to a date.
- DATE_SUB() subtracts a specified time interval from a date.
- DATEDIFF() returns the number of days between two dates.
Adding and Subtracting Dates
Use DATE_ADD() and DATE_SUB() to manipulate dates by adding or subtracting days, months, years, or other intervals.
- Syntax example: DATE_ADD(date, INTERVAL value unit)
- Units can be DAY, MONTH, YEAR, HOUR, MINUTE, SECOND, etc.
Calculating Date Differences
DATEDIFF() calculates the difference in days between two dates, useful for age or duration calculations.
Formatting Dates
DATE_FORMAT() allows you to format date and time values into readable strings.
- You can specify format specifiers like %Y for year, %m for month, %d for day.
- This is useful for displaying dates in different formats or locales.
| Specifier | Description | Example Output |
|---|---|---|
| %Y | Year as four digits | 2024 |
| %m | Month as two digits | 06 |
| %d | Day of the month | 15 |
| %H | Hour (24-hour) | 14 |
| %i | Minutes | 30 |
| %s | Seconds | 05 |
| %W | Weekday name | Monday |
Practical Example
Returns the current date and time, e.g., '2024-06-15 14:30:05'.
Extracts the year (2024) and month (6) from the given date.
Returns '2024-06-25' by adding 10 days to the date.
Formats the date as '15-06-2024'.
Examples
SELECT NOW();Returns the current date and time, e.g., '2024-06-15 14:30:05'.
SELECT YEAR('2024-06-15'), MONTH('2024-06-15');Extracts the year (2024) and month (6) from the given date.
SELECT DATE_ADD('2024-06-15', INTERVAL 10 DAY);Returns '2024-06-25' by adding 10 days to the date.
SELECT DATE_FORMAT('2024-06-15', '%d-%m-%Y');Formats the date as '15-06-2024'.
Best Practices
- Always use the appropriate date function for your specific need to avoid errors.
- Use DATE_FORMAT() to present dates in user-friendly formats.
- Be mindful of time zones when working with date and time functions.
- Use parameterized queries to safely handle date inputs in applications.
Common Mistakes
- Confusing string manipulation with date functions for date operations.
- Ignoring time zones leading to incorrect date/time values.
- Using deprecated functions or incorrect syntax for date intervals.
- Not validating date inputs before processing.
Hands-on Exercise
Calculate Days Between Two Dates
Write a query to find the number of days between '2024-01-01' and '2024-06-15'.
Expected output: 166
Hint: Use the DATEDIFF() function.
Format Current Date
Format the current date as 'Month Day, Year' (e.g., June 15, 2024).
Expected output: June 15, 2024
Hint: Use DATE_FORMAT() with appropriate specifiers.
Add One Month to a Given Date
Write a query to add one month to '2024-06-15'.
Expected output: 2024-07-15
Hint: Use DATE_ADD() with INTERVAL 1 MONTH.
Interview Questions
What function would you use to get the current date and time in MySQL?
InterviewYou would use the NOW() function to get the current date and time.
How can you add 5 days to a date in MySQL?
InterviewUse DATE_ADD(date, INTERVAL 5 DAY) to add 5 days to a date.
Which function extracts the year from a date in MySQL?
InterviewThe YEAR() function extracts the year part from a date.
MCQ Quiz
1. What is the best first step when learning Date Functions?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce Date Functions?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. MySQL date functions allow you to manipulate, format, and extract information from date and time values stored in your database.
B. Date Functions never needs examples
C. Date Functions is unrelated to practical work
D. Date Functions should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- MySQL provides a rich set of functions to handle dates and times.
- Functions like NOW(), CURDATE(), and DATE_FORMAT() are essential for date operations.
- Extracting parts of a date (year, month, day) is straightforward with dedicated functions.
- Date arithmetic can be performed using functions like DATE_ADD() and DATE_SUB().
- MySQL date functions allow you to manipulate, format, and extract information from date and time values stored in your database.
Summary
MySQL date functions provide powerful tools to work with date and time data efficiently.
Understanding how to retrieve, manipulate, and format dates is essential for building robust database applications.
Practice using these functions to become proficient in handling date-related queries.
Frequently Asked Questions
What is the difference between NOW() and CURDATE()?
NOW() returns the current date and time, while CURDATE() returns only the current date without the time.
How do I format a date in MySQL?
Use the DATE_FORMAT() function with format specifiers to display dates in the desired format.
Can I perform arithmetic operations on dates in MySQL?
Yes, functions like DATE_ADD() and DATE_SUB() allow you to add or subtract time intervals from dates.





