In this comprehensive guide, we’ll cover various aspects of SQL dates, including date functions, comparison operations and formatting.

SQL Dates: Data Types
SQL supports various date-related data types, depending on the level of precision and range required:
- DATE: Stores a date (year, month, and day).
- TIME: Stores a time of day (hour, minute, second).
- DATETIME or TIMESTAMP: Stores both date and time.
Date Functions
SQL provides a range of date functions to work with date and time values. Here are some common date functions:
- CURRENT_DATE(): Returns the current date.
- CURRENT_TIME(): Returns the current time.
- CURRENT_TIMESTAMP(): Returns the current date and time.
- DATE_FORMAT(): Formats a date as a string.
- DATE_ADD() and DATE_SUB(): Add or subtract intervals from a date.
- EXTRACT(): Extracts date components like year, month, or day from a date.
Date Comparison Operators
Date values can be compared using various operators:
- Equal To (=): Compares if two date values are equal.
- Not Equal To (!= or <>): Checks if two dates are not equal.
- Greater Than (>): Compares if the left date is later than the right date.
- Less Than (<): Compares if the left date is earlier than the right date.
- Greater Than or Equal To (>=): Checks if the left date is later than or equal to the right date.
- Less Than or Equal To (<=): Checks if the left date is earlier than or equal to the right date.
Date Ranges Using BETWEEN
The BETWEEN operator is used to specify a range of values. You can use it to filter date values within a specified range.
Example:
Date Formatting
Date formatting is essential for displaying date values in a human-readable format. You can use the DATE_FORMAT()
function or database-specific formatting functions to achieve this.
Examples:
Conclusion: SQL Dates
In conclusion, this comprehensive guide has provided a detailed exploration of SQL dates, covering essential aspects such as date data types, date functions, date comparison operators, and date formatting. Here are the key takeaways:
- Date Data Types: SQL offers various date-related data types, including DATE, TIME, and DATETIME (or TIMESTAMP), to suit different precision and range requirements. Examples demonstrated how to create tables with these data types and insert date, time, and datetime values into them.
- Date Functions: SQL provides a range of date functions like CURRENT_DATE(), CURRENT_TIME(), CURRENT_TIMESTAMP(), DATE_FORMAT(), DATE_ADD(), DATE_SUB(), and EXTRACT(). These functions enable manipulation and extraction of date and time components for various use cases.
- Date Comparison Operators: Date values can be compared using operators such as equal to (=), not equal to (!= or <>), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). Examples illustrated how to use these operators to filter data based on date conditions.
- Date Ranges Using BETWEEN: The BETWEEN operator is useful for specifying date ranges. It allows you to filter date values falling within a specified range, as demonstrated in an example filtering orders within a specific month.
- Date Formatting: Proper date formatting is crucial for presenting date values in a human-readable format. The DATE_FORMAT() function and database-specific formatting functions were discussed, with an example showcasing date formatting in MySQL.
Understanding SQL dates and how to work with them is fundamental for database operations involving time-sensitive data.
Related Articles: