Date Functions

SQL date functions let you extract parts of a date, calculate differences between dates, and format date values - essential for any time-based analysis like monthly reports or cohort tracking.

Common Date Functions

  • NOW() / CURRENT_DATE - returns the current date and time
  • YEAR() / MONTH() / DAY() - extract individual date parts
  • DATEDIFF() - calculates the difference between two dates
  • DATE_ADD() / DATE_SUB() - add or subtract a time interval from a date
  • DATE_FORMAT() - formats a date into a custom string representation

Examples

-- Customers who signed up this year
SELECT * FROM customers
WHERE YEAR(signup_date) = YEAR(CURRENT_DATE);

-- Number of days since signup
SELECT first_name, DATEDIFF(CURRENT_DATE, signup_date) AS days_since_signup
FROM customers;

-- Orders placed in the last 30 days
SELECT * FROM orders
WHERE order_date >= DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY);

-- Format a date as "15 Jul 2026"
SELECT DATE_FORMAT(signup_date, '%d %b %Y') FROM customers;
Date function names and formatting codes differ noticeably between database systems (MySQL, PostgreSQL, SQL Server all vary) - it's worth bookmarking the reference for whichever database you're working with.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course