How to Add Dates in SQL: MySQL, PostgreSQL, SQL Server

DbVisualizerDbVisualizer
2 min read

When working with SQL, you’ll often need to manipulate dates—think setting due dates, generating expiration timelines, or adjusting schedules. Different databases handle this differently, and knowing the correct syntax saves time and headaches. In this article, we’ll explore how to add and subtract dates in MySQL, PostgreSQL, and SQL Server with clear examples for each.

Adding Dates in SQL – Real Use Cases

Use cases for date arithmetic include:

  • Adding 30 days to an order date to set delivery expectations

  • Subtracting months from a timestamp to retrieve historical trends

  • Scheduling email campaigns or notifications

  • Calculating contract durations for employees

Now let’s look at how each database handles these operations.

MySQL: DATE_ADD and DATE_SUB

MySQL syntax for adding and subtracting intervals:

SELECT DATE_ADD('2024-10-01', INTERVAL 30 DAY);
-- Adds 30 days: returns '2024-10-31'

To subtract:

SELECT DATE_SUB('2024-10-01', INTERVAL 10 DAY);

You can also use:

SELECT DATE_ADD('2024-10-01', INTERVAL -10 DAY);

If you pass NULL, the result is also NULL.

PostgreSQL: Using + and - Operators

PostgreSQL uses arithmetic operators directly:

SELECT DATE '2024-01-15' + INTERVAL '10 days';
-- Returns: '2024-01-25 00:00:00'

To subtract:

SELECT DATE '2024-04-01' - INTERVAL '2 months';
-- Returns: '2024-02-01 00:00:00'

PostgreSQL is more flexible but requires the INTERVAL keyword and often returns timestamp types.

SQL Server: DATEADD Function

SQL Server uses a three-argument structure:

SELECT DATEADD(DAY, 30, '2024-10-01');
-- Adds 30 days

To subtract, just pass a negative number:

SELECT DATEADD(YEAR, -1, '2024-12-31');

No DATE_SUB() here—use DATEADD() with negative intervals.

FAQ

Which function is most common across platforms?

Each system has its own: DATE_ADD() in MySQL, + INTERVAL in PostgreSQL, and DATEADD() in SQL Server. Learn each as needed.

Can I format the result of a date calculation?

Yes. Use DATE_FORMAT() in MySQL, TO_CHAR() in PostgreSQL, and FORMAT() in SQL Server to customize output.

Do these functions work with columns too?

Absolutely. You can use column names in place of static dates. For example:

SELECT DATE_ADD(order_date, INTERVAL 7 DAY) FROM orders;

What SQL tool makes this easier?

Try DbVisualizer—a visual SQL client that supports all major DBs, great for working with date operations interactively.

Conclusion

Adding dates in SQL is more than a syntax trick—it’s a key skill for managing business logic, schedules, and data pipelines. Once you understand how each system handles it, you can adapt your queries across platforms with ease. Whether you're using MySQL's DATE_ADD, PostgreSQL's interval operators, or SQL Server's DATEADD, you now have what you need to calculate time like a pro.

Read Adding Dates in SQL: A Complete Tutorial for more info.

0
Subscribe to my newsletter

Read articles from DbVisualizer directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

DbVisualizer
DbVisualizer

DbVisualizer is the database client with the highest user satisfaction. It is used for development, analytics, maintenance, and more, by database professionals all over the world. It connects to all popular databases and runs on Win, macOS & Linux.