T-sql Dateadd

Galaxy Glossary

How do you add specific time intervals to a date or time value in T-SQL?

The `DATEADD` function in T-SQL is used to add a specified time interval (like days, hours, minutes, seconds) to a date or time value. It's crucial for calculations involving time-based data, such as scheduling, reporting, and tracking durations.
Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

The `DATEADD` function in T-SQL is a powerful tool for manipulating date and time values. It allows you to add specific time intervals, such as days, hours, minutes, seconds, milliseconds, months, or years, to a given date or time. This is essential for tasks like calculating future dates, determining elapsed time, or generating reports based on time-related criteria. For example, you might want to calculate the date three months from now, or determine the date one week before a specific event. `DATEADD` provides the flexibility to perform these calculations. It's a fundamental function for any SQL developer working with temporal data. Understanding `DATEADD` is crucial for tasks involving scheduling, reporting, and tracking durations. The function takes three arguments: the interval to add, the number of intervals to add, and the date or time value to which to add the interval.

Why T-sql Dateadd is important

The `DATEADD` function is essential for any SQL developer working with time-sensitive data. It's crucial for tasks like scheduling, reporting, calculating durations, and tracking events over time. Its versatility makes it a fundamental tool for manipulating temporal data in databases.

Example Usage


-- Calculate the date three days from now
SELECT DATEADD(day, 3, GETDATE());

-- Calculate the date one week before a specific date
DECLARE @SpecificDate DATE = '2024-10-27';
SELECT DATEADD(day, -7, @SpecificDate);

-- Add 2 hours to a time value
SELECT DATEADD(hour, 2, '10:00:00');

-- Add 15 minutes to a datetime value
SELECT DATEADD(minute, 15, GETDATE());

-- Add 3 months to a date
SELECT DATEADD(month, 3, '2024-01-15');

-- Add 2 years to a date
SELECT DATEADD(year, 2, '2024-01-15');

Common Mistakes

Want to learn about other SQL terms?