sql / intermediate
Snippet
Performing Date Calculations with the INTERVAL Data Type
ANSI SQL provides the INTERVAL data type to represent temporal spans. Combining timestamps or dates with INTERVAL values enables clean, standardized date arithmetic across queries.
snippet.sql
sql
1
2
3
4
5
SELECT subscription_id,start_date,start_date + INTERVAL '30' DAY AS renewal_dateFROM subscriptionsWHERE start_date + INTERVAL '1' YEAR > CURRENT_DATE;
Breakdown
1
SELECT subscription_id,
Selects identifier column from subscriptions table.
2
start_date,
Retrieves the initial start date of the user subscription.
3
start_date + INTERVAL '30' DAY AS renewal_date
Adds a 30-day temporal duration to the start date using the ANSI SQL INTERVAL type.
4
FROM subscriptions
Specifies subscriptions as the target table.
5
WHERE start_date + INTERVAL '1' YEAR > CURRENT_DATE;
Filters for active subscriptions that started within the past year relative to current date.