SQL Date & Time Functions: Intervals, Formatting & Calculations

Tracking time is central to modern software applications: user registration dates, order timestamps, subscription renewal periods, and loan interest schedules. SQL includes robust Date and Time Functions for manipulating calendar intervals and time zones.


Core SQL Date Functions Reference

Function & Syntax Operation Performed Example & Output
NOW() / CURRENT_TIMESTAMP Returns current system date and time. '2026-11-06 20:54:49'
CURDATE() / CURRENT_DATE Returns current calendar date only. '2026-11-06'
DATE_ADD(date, INTERVAL expr unit) Adds an interval (days, months, years) to a date. DATE_ADD('2026-01-01', INTERVAL 30 DAY)
DATEDIFF(date1, date2) Returns integer days between date1 - date2. DATEDIFF('2026-11-06', '2026-11-01') → 5
EXTRACT(unit FROM date) Extracts YEAR, MONTH, DAY, HOUR. EXTRACT(YEAR FROM NOW()) → 2026
DATE_FORMAT(date, format) Formats date into custom string layout. DATE_FORMAT(NOW(), '%W, %M %Y')

Hands-on Query: Calculating Student Seniority & Age

-- Calculate total enrolled days and extract graduation year
SELECT 
    student_id,
    first_name,
    enrollment_date,
    DATEDIFF(CURRENT_DATE, enrollment_date) AS days_enrolled,
    DATE_ADD(enrollment_date, INTERVAL 3 YEAR) AS expected_graduation_date
FROM students;

Next, let us learn conditional logic using the SQL CASE Expression!

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!