How to Add Time to a DateTime Value in SQL

To add time to a datetime value in SQL, use the DATEADD() function. This function takes three arguments: the unit of time you want to add (e.g. minutes, hours, days, etc.), the amount of time you want to add, and the datetime value you want to add the time to. For example, if you wanted to add 10 minutes to a datetime value, you would use the following query:

SELECT DATEADD(MINUTE, 10, datetime_value) FROM table_name;

Examples

Let's look at a few examples of how this function can be used. Suppose you have a table called events with the following data:

event_name start_time
Party 2020-01-01 10:00:00
Meeting 2020-01-01 11:00:00

If you wanted to add 30 minutes to the start time of the Party event, you would use the following query:

SELECT DATEADD(MINUTE, 30, start_time) FROM events WHERE event_name = 'Party';

This query would return the result 2020-01-01 10:30:00, which is the start time of the Party event with 30 minutes added.

Additional Info

The DATEADD() function is supported by most major databases, including MySQL, PostgreSQL, and SQL Server. However, the syntax may vary slightly depending on the database you are using. For more information, check out the documentation for your particular database. 🤓

Want to build your own LLM Apps with AirOps👇👇