Back to Library

DATEADD() in SQL

Adds or subtracts a specified time interval from a date value.

Syntax

DATEADD(interval_type, interval_value, date_expression) 
-- Some databases use: DATE_ADD() or datetime + INTERVAL
Return type
DATE/DATETIME/TIMESTAMP (same as input date type)

DATEADD() Function Example

SELECT DATEADD(month, 3, order_date) FROM orders; 
-- Output: '2024-03-26' (adds 3 months to order_date)

What is DATEADD() in SQL?

The DATEADD() function in SQL is used to add or subtract a specific time interval (such as days, months, or years) to a given date value. It is commonly used for date calculations, scheduling tasks, and adjusting timestamps dynamically.

This function is primarily available in SQL Server. Other databases use different equivalents:

  • MySQL & PostgreSQL: DATE_ADD() and DATE_SUB().

  • Oracle: Uses direct date arithmetic (SYSDATE + INTERVAL '1' DAY).

The DATEADD() function requires three parameters:

  1. Date part (e.g., YEAR, MONTH, DAY, HOUR).

  2. Value to add/subtract (negative for subtraction).

  3. Starting date.

It is widely used in reporting, forecasting, and calculating expiration or due dates.

Parameters:

  • interval_type: Unit of time (year, month, day, hour, minute, second)

  • interval_value: Number of intervals to add (negative for subtraction)

  • date_expression: Starting date value

Example Use Cases:

-- Calculate due date

SELECT invoice_id, order_date, 
       DATEADD(day, 30, order_date) AS due_date 
FROM invoices;
-- Output:
invoice_id    order_date     due_date
----------    ----------     ----------
INV001        2024-12-28    2025-01-27
INV002        2024-12-27    2025-01-26
INV003        2024-12-26    2025-01-25
INV004        2024-12-25    2025-01-24
INV005        2024-12-24    2025-01-23

-- Find orders from last week 

SELECT * FROM orders WHERE order_date > DATEADD(day, -7, CURRENT_DATE); 
-- Output:
order_id    customer_id    order_date     amount    status
--------    -----------    ----------     ------    -------
1001        C123          2024-12-28     599.99    Shipped
1002        C456          2024-12-27     299.50    Processing
1003        C789          2024-12-26     749.99    Pending
1004        C234          2024-12-25     199.99    Delivered
1005        C567          2024-12-24     449.99    Shipped

-- Schedule future maintenance

SELECT equipment_id, last_service_date,
       DATEADD(month, 6, last_service_date) AS next_service 
FROM equipment;

-- Output:
equipment_id    last_service_date    next_service
------------    -----------------    ------------
EQ001           2024-12-15          2025-06-15
EQ002           2024-11-30          2025-05-30
EQ003           2024-12-01          2025-06-01
EQ004           2024-12-10          2025-06-10
EQ005           2024-12-20          2025-06-20

Notes:

  • Behavior: Returns NULL if any parameter is NULL

  • Performance Considerations: Date calculations may cross calendar boundaries

  • Version Info: Syntax varies significantly by DBMS

  • Deprecated/Recommended Alternatives: None

Error Handling:

  • Error: Returns NULL or error for invalid dates

  • Recommendation: Validate input dates before calculation

Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

DATE_ADD(), + INTERVAL

DATE_ADD(date, INTERVAL 1 DAY)

Returns NULL

PostgreSQL

+ INTERVAL

date + INTERVAL '1 day'

Returns NULL

SQL Server

DATEADD()

DATEADD(day, 1, date)

Returns NULL

SQLite

datetime(), julianday()

date(date, '+1 day')

Returns NULL

BigQuery

DATE_ADD(), DATETIME_ADD()

DATE_ADD(date, INTERVAL 1 DAY)

Returns NULL

Snowflake

DATEADD()

DATEADD(day, 1, date)

Returns NULL

Athena

DATE_ADD()

DATE_ADD(day, 1, date)

Returns NULL

Related Functions:

One place for all your queries,
directly on your SQL editor