SQL Date and Time Data Types | Complete Beginner's Guide
1. DATE Data Type
The DATE type is used to store calendar dates (year, month, and day) without any time component.
Key Features:
- Format: 'YYYY-MM-DD'
- Range: From '1000-01-01' to '9999-12-31'
Syntax:
column_name DATE;
Example:
CREATE TABLE employees (
id INT,
hire_date DATE
);
Sample value: 2025-04-26
When to Use DATE:
- Birthdays
- Join dates
- Appointment days
- Event dates
2. TIME Data Type
The TIME type stores only the time of day — without any reference to dates.
Key Features:
- Format: 'HH:MM:SS'
- Range: From '-838:59:59' to '838:59:59' (in MySQL)
Syntax:
column_name TIME;
Example:
CREATE TABLE work_shifts (
id INT,
shift_start TIME,
shift_end TIME
);
Sample value: 14:30:00
When to Use TIME:
- Store working hours
- Record start and end times
- Store durations
3. DATETIME Data Type
The DATETIME type combines both date and time into a single field.
Key Features:
- Format: 'YYYY-MM-DD HH:MM:SS'
- Range: From '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
Syntax:
column_name DATETIME;
Example:
CREATE TABLE bookings (
id INT,
booking_time DATETIME
);
Sample value: 2025-04-26 15:45:00
When to Use DATETIME:
- Recording exact timestamps (orders, events)
- Scheduling meetings
- Storing logs with full date and time
4. TIMESTAMP Data Type
The TIMESTAMP type also stores date and time, but it is typically used for recording the exact moment an event happens, especially with automatic time tracking.
Key Features:
- Format: 'YYYY-MM-DD HH:MM:SS'
- Range: From '1970-01-01 00:00:01 UTC' to '2038-01-19 03:14:07 UTC'
- Often updates automatically (e.g., current time by default)
Syntax:
column_name TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
Example:
CREATE TABLE user_logins (
id INT,
login_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Sample value: 2025-04-26 15:45:00
When to Use TIMESTAMP:
- Track record creation or update times
- Automatically capture the moment of an event
- For web applications where you need to log changes
5. YEAR Data Type
The YEAR type stores only a year value.
Key Features:
- Format: 'YYYY'
- Range: From '1901' to '2155' (MySQL)
Syntax:
column_name YEAR;
Example:
CREATE TABLE models (
id INT,
model_year YEAR
);
Sample value: 2025
When to Use YEAR:
- Storing production years
- Academic years
- Model years (cars, products)
Quick Comparison Table
Data Type | Stores | Format | Best For |
---|---|---|---|
DATE | Only date | YYYY-MM-DD | Birthdays, event dates |
TIME | Only time | HH:MM:SS | Work hours, time records |
DATETIME | Date + time | YYYY-MM-DD HH:MM:SS | Orders, meetings, appointments |
TIMESTAMP | Date + time (UTC) | YYYY-MM-DD HH:MM:SS | Logging creation/update times |
YEAR | Only year | YYYY | Model years, academic years |
Important Tips for Working with Date/Time Types
- Always use DATETIME when you need both date and time without timezone handling
- Use TIMESTAMP if you want automatic updates based on the current time (especially in MySQL)
- Remember that TIMESTAMP has a limited range (up to 2038)
- For heavy date/time queries, indexing the date fields can greatly improve performance
- Consider timezone implications when storing timestamps for global applications
- Use appropriate date/time functions for calculations rather than manual operations
Conclusion
Choosing the right date and time type ensures your data remains accurate, searchable, and easy to manage. Each type serves specific purposes:
- DATE for calendar dates without time
- TIME for time-of-day values
- DATETIME for complete date+time storage
- TIMESTAMP for automatic recording of events
- YEAR for simple year storage
Understanding these differences will help you design more efficient databases and write better SQL queries for your applications.