Working with Date and Time Functions in MySQL

MySQL provides a variety of date and time functions that allow you to perform various operations and calculations on date and time values. These functions can be helpful when working with date and time data types in your MySQL databases. In this article, we will explore some common date and time functions in MySQL and how to use them effectively.

1. CURDATE() and CURTIME()

The CURDATE() function returns the current date, while the CURTIME() function returns the current time. These functions are useful when you need to retrieve the current date or time for various purposes, such as inserting a timestamp into a table or performing calculations.

Example usage:

SELECT CURDATE() AS Current_Date, CURTIME() AS Current_Time;

Output:

| Current_Date | Current_Time |
|--------------|--------------|
| 2022-08-05   | 15:30:45     |

2. DATE(), TIME(), and TIMESTAMP()

The DATE(), TIME(), and TIMESTAMP() functions allow you to extract specific parts of a date or time value. The DATE() function extracts the date portion, the TIME() function extracts the time portion, and the TIMESTAMP() function extracts both the date and time portions of a datetime value.

Example usage:

SELECT
    DATE('2022-08-05 15:30:45') AS Extracted_Date,
    TIME('2022-08-05 15:30:45') AS Extracted_Time,
    TIMESTAMP('2022-08-05 15:30:45') AS Extracted_Timestamp;

Output:

| Extracted_Date | Extracted_Time | Extracted_Timestamp       |
|----------------|----------------|---------------------------|
| 2022-08-05     | 15:30:45       | 2022-08-05 15:30:45       |

3. DATE_FORMAT()

The DATE_FORMAT() function allows you to format a date or time value according to a specified format string. The format string defines how the output should be formatted, including elements such as year, month, day, hour, minute, etc.

Example usage:

SELECT DATE_FORMAT('2022-08-05', '%Y-%m-%d') AS Formatted_Date;

Output:

| Formatted_Date |
|----------------|
| 2022-08-05     |

4. DATE_ADD() and DATE_SUB()

The DATE_ADD() and DATE_SUB() functions allow you to add or subtract a specific time interval from a date or time value. You can use various interval options such as seconds, minutes, hours, days, months, years, etc., to perform the desired calculation.

Example usage:

SELECT
    DATE_ADD('2022-08-05', INTERVAL 1 DAY) AS Added_Date,
    DATE_SUB('2022-08-05', INTERVAL 1 MONTH) AS Subtracted_Date;

Output:

| Added_Date  | Subtracted_Date |
|-------------|-----------------|
| 2022-08-06  | 2022-07-05      |

Conclusion

Working with date and time functions in MySQL can greatly simplify the handling and manipulation of date and time values in your databases. This article introduced some commonly used date and time functions like CURDATE(), CURTIME(), DATE(), TIME(), TIMESTAMP(), DATE_FORMAT(), DATE_ADD(), and DATE_SUB(). By utilizing these functions effectively, you can perform various calculations, formatting, and retrieval of date and time values in a much simpler and efficient manner.


noob to master © copyleft