I was recently asked to add a field to a SQL report that adds days to an existing date field. I thought I’d share the process of how to use the DATEADD function in SQL.
A DATEADD() function is placed in the SELECT statement and consists of the following: DATEADD(datepart, number, date)
Click the tabs to view definitions:
Now that we understand the DATEADD() function, let’s put it to use. I’ll use the DATEADD function I created to provide a real life example.
I was asked to create two separate fields that added 20 days and 30 days to a patient’s admission date. The DATEADD functions I created were:
I set the datepart to day, number to 20 & 30 (which represented the number of days) and the date field in my database table was called AdmissionDate.
A brief example of what this query would look like is:
A sample result would look like: