πŸ—“οΈ SQL Utilities & Features
Estimated reading: 3 minutes 28 views

πŸ•’ SQL Dates – Handle, Filter, and Format Date/Time in SQL

🧲 Introduction – Why Master SQL Dates?

Working with dates in SQL is essential for time-based filtering, reporting, scheduling, and data analysis. SQL supports robust date and time handling across various functions and formats.

🎯 In this guide, you’ll learn:

  • Common SQL date types and functions
  • How to filter, format, and manipulate dates
  • Cross-platform differences and best practices

πŸ“… 1. SQL Date/Time Data Types

TypeDescription
DATECalendar date (YYYY-MM-DD)
TIMETime of day (HH:MM:SS)
DATETIMEDate and time combined (MySQL)
TIMESTAMPDate/time with timezone or epoch info
INTERVALDuration between two times (PostgreSQL)

πŸ› οΈ 2. Insert and Select Dates

INSERT INTO events (event_date) VALUES ('2025-12-25');
SELECT * FROM events WHERE event_date = '2025-12-25';

βœ… Always use YYYY-MM-DD format for compatibility.


⏱️ 3. Common Date Functions

FunctionDescription
NOW()Current date and time
CURRENT_DATEToday’s date
DATE_ADD()Add interval (MySQL)
DATE_SUB()Subtract interval (MySQL)
DATE_PART()Extract year/month/day (PostgreSQL)
EXTRACT()Extract field from date/time
DATEDIFF()Days between two dates (MySQL/SQL Server)
AGE()Difference between dates (PostgreSQL)

πŸ” 4. Filtering with Dates

-- Find events after today
SELECT * FROM events WHERE event_date > CURRENT_DATE;

-- Events within 7 days
SELECT * FROM events
WHERE event_date BETWEEN CURRENT_DATE AND DATE_ADD(CURRENT_DATE, INTERVAL 7 DAY);

βœ… Useful for scheduling, reminders, time-based alerts.


πŸ“ 5. Formatting Dates

DBMSFunction Example
MySQLDATE_FORMAT(date_col, '%Y-%m-%d')
PostgreSQLTO_CHAR(date_col, 'YYYY-MM-DD')
SQL ServerFORMAT(date_col, 'yyyy-MM-dd')

βœ… Format dates for reports, exports, or readable output.


πŸ“˜ Best Practices

βœ… Recommended❌ Avoid This
Use YYYY-MM-DD for date literalsUsing ambiguous formats (MM/DD/YY)
Use built-in functions for consistencyManually calculating intervals
Use indexed datetime columns for filteringUsing functions on the column side of WHERE

πŸ“Œ Summary – Recap & Next Steps

Date and time functions in SQL allow powerful temporal analysis across datasets. Whether you’re filtering by timeframes, comparing durations, or formatting output, date handling is a core skill.

πŸ” Key Takeaways:

  • Understand DATE, TIME, DATETIME, TIMESTAMP types
  • Use functions like NOW(), DATE_ADD(), and DATEDIFF() for manipulation
  • Format and filter time ranges with built-in utilities

βš™οΈ Real-World Relevance:
Used in scheduling apps, billing cycles, attendance systems, time series analytics, and log processing.


❓ FAQ – SQL Dates

❓ How do I get the current date in SQL?

βœ… Use CURRENT_DATE or NOW() depending on whether you want date or datetime.

❓ How do I calculate the number of days between two dates?

βœ… Use DATEDIFF() in MySQL/SQL Server or AGE() in PostgreSQL.

❓ How do I extract year or month from a date?

βœ… Use EXTRACT(YEAR FROM date_column) or DATE_PART('month', date_column).

❓ Can I compare just the date part of a timestamp?

βœ… Yes. Use CAST(timestamp_col AS DATE) or extract the ::DATE portion.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ•’ SQL DATES

Or Copy Link

CONTENTS
Scroll to Top