๐ SQL Date & Time Functions โ Working with Dates, Time & Time-Based Analytics
Almost every real-world analytics project involves dates.
Examples:
โข When was the order placed?
โข How many days did delivery take?
โข What was the revenue in January?
โข Which month had the highest sales?
โข How many customers purchased this year?
โข How long has an account been inactive?
โข What is the month-over-month growth?
โข Which transactions breached the SLA?
SQL provides powerful date and time functions to answer these questions.
๐ง 1. Common Date & Time Data Types
Different databases support different date/time types, but commonly you'll encounter:
โข DATE
โข TIME
โข TIMESTAMP
โข DATETIME
DATE stores only the date:
2026-09-18TIME stores only time:
14:30:25TIMESTAMP stores both date and time:
2026-09-18 14:30:25 The exact data types and behavior vary by database.
๐ 2. Current Date
Many databases provide a function for the current date.
For example:
SELECT CURRENT_DATE;
Result:
2026-09-18The exact function can vary by SQL dialect.
โฐ 3. Current Date and Time
A common SQL expression is:
SELECT CURRENT_TIMESTAMP;
It returns the current date and time.
For example:
2026-09-18 21:39:00Again, exact formatting depends on the database.
๐ 4. Extracting Year, Month & Day
Suppose:
order_date = 2026-09-18We may want: Year โ 2026, Month โ 9, Day โ 18
A common SQL approach is:
SELECT
EXTRACT(YEAR FROM order_date) AS year,
EXTRACT(MONTH FROM order_date) AS month,
EXTRACT(DAY FROM order_date) AS day
FROM orders;
Some databases use functions such as
YEAR(order_date), MONTH(order_date), DAY(order_date). So always check the syntax for your SQL dialect.
๐ 5. Revenue by Year
Suppose we want annual revenue.
SELECT
EXTRACT(YEAR FROM order_date) AS order_year,
SUM(amount) AS revenue
FROM orders
GROUP BY EXTRACT(YEAR FROM order_date)
ORDER BY order_year;
Result:
โข 2024 | 850000
โข 2025 | 1120000
โข 2026 | 1380000
This lets us analyze year-over-year business performance.
๐ 6. Revenue by Month
We can group transactions by month.
SELECT
EXTRACT(YEAR FROM order_date) AS year,
EXTRACT(MONTH FROM order_date) AS month,
SUM(amount) AS revenue
FROM orders
GROUP BY
EXTRACT(YEAR FROM order_date),
EXTRACT(MONTH FROM order_date)
ORDER BY
year,
month;
Important: Don't group only by month number if your data spans multiple years.
For example January 2025 and January 2026 both have
month = 1. Grouping only by month could incorrectly combine them.๐๏ธ 7. Month-Level Grouping
Some databases provide functions that truncate a date to the beginning of a month.
For example, PostgreSQL:
SELECT
DATE_TRUNC('month', order_date) AS month,
SUM(amount) AS revenue
FROM orders
GROUP BY DATE_TRUNC('month', order_date)
ORDER BY month;
This produces values such as
2026-01-01, 2026-02-01 representing each month. This is often convenient for time-series analysis.๐ 8. Month-over-Month Analysis
Suppose we have monthly revenue: Jan 100000, Feb 120000, Mar 110000. We can use
LAG():WITH monthly_sales AS (
SELECT
DATE_TRUNC('month', order_date) AS month,
SUM(amount) AS revenue
FROM orders
GROUP BY DATE_TRUNC('month', order_date)
)
SELECT
month,
revenue,
LAG(revenue) OVER (ORDER BY month) AS previous_month_revenue
FROM monthly_sales;
๐ 9. Month-over-Month Growth %
We can extend the previous query: