sql / intermediate
Snippet
Standardized Date Arithmetic Using Temporal Datatypes and INTERVAL
ANSI SQL provides standardized temporal types and the INTERVAL keyword to perform clean date and time arithmetic without relying on vendor-specific functions. Combining timestamps with INTERVAL expressions allows precise duration calculations.
snippet.sql
sql
1
2
3
4
5
6
7
SELECTsubscription_id,user_id,start_date,start_date + INTERVAL '30' DAY AS renewal_dateFROM user_subscriptionsWHERE expiration_date < CURRENT_TIMESTAMP;
Breakdown
1
SELECT
Begins the temporal calculation query.
2
subscription_id,
Selects the subscription primary identifier.
3
user_id,
Retrieves the associated user identifier.
4
start_date,
Fetches the original subscription initiation timestamp.
5
start_date + INTERVAL '30' DAY AS renewal_date
Adds a duration of 30 days to start_date using standard INTERVAL arithmetic.
6
FROM user_subscriptions
Queries the user subscriptions data store.
7
WHERE expiration_date < CURRENT_TIMESTAMP;
Filters for records expiring prior to the system's execution timestamp.