Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)
closed by

Actually I tried to use the DATEADD function to add a quarter to a date. My environment is MS Management Studio (v17.7) and SQL Server Express (14.0.1000). Based in the UK.

I am using the code down below:

SELECT DATEADD(q,1,'1997-09-30')

 

I am expecting a return value of '1997-12-31 00:00:00.000' but I am getting '1997-12-30 00:00:00.000) ie. 30th of the month rather than 31st.

I have tried to change the parameters:

SELECT DATEADD(q,2,'1997-09-30') - returns 1998-03-30 00:00:00.000 (I would expect 1998-03-31 00:00:00.000)

SELECT DATEADD(qq,1,'1997-09-30') / SELECT DATEADD(quarter,1,'1997-09-30') - same result

I have also tried to use a parameter:

declare @date datetime (also tried datetime2, smalldate, date)

set @date = ('1997-09-30')

SELECT DATEADD(q,1,@date) 

Please tell me where I am going wrong and help me out with this.

closed

4 Answers

0 votes
by (11.7k points)
selected by
 
Best answer

Well, A quarter is defined as 3 months. So why do you expect 3 months after 1997-09-30 to not be 1997-12-30? It can’t be expected DATEADD(q,1,'20000101') to return '2000-06-30' or '2000-03-31' would you?

If you need the end of the month for 3 months after a specific date, you can use EOMONTH:

SELECT EOMONTH(DATEADD(QUARTER,1,'19970930'));

If you want to get more insights of SQL, checkout this SQL Course from Intellipaat.

0 votes
by (13k points)

The issue you're encountering with the `DATEADD` function is related to the behavior of quarter calculations in SQL Server. The `DATEADD` function with the 'q' or 'qq' parameter does not always return the exact quarter end date you expect.

In SQL Server, the 'q' parameter used with `DATEADD` calculates the number of quarters based on an integer input. However, it does not necessarily align the result with the end of the quarter. Instead, it adds the specified number of quarters to the given date.

To ensure that you get the exact quarter end date, you can use an alternative approach. One option is to use the `EOMONTH` function to find the end of the month and then adjust it to the end of the quarter.

Here's an example of how you can achieve this:

DECLARE @inputDate DATE = '1997-09-30';

-- Find the end of the month

DECLARE @endOfMonth DATE = EOMONTH(@inputDate);

-- Calculate the end of the quarter

DECLARE @endOfQuarter DATE = DATEADD(DAY, -1, DATEADD(MONTH, (MONTH(@endOfMonth) - 1) / 3 * 3 + 3, DATEFROMPARTS(YEAR(@endOfMonth), 1, 1)));

SELECT @endOfQuarter;

In this example, the `EOMONTH` function is used to find the end of the month for the given input date. Then, using the `DATEADD` function along with some calculations, the end of the quarter is determined by subtracting one day from the calculated quarter-end month.

Running the above code will yield the result you expect: '1997-12-31'.

By utilizing this alternative approach, you can reliably obtain the quarter end date in SQL Server.

0 votes
by (11.4k points)
The behavior you're observing with `DATEADD` is expected, as it follows the rules of SQL Server's date calculations. When adding quarters (`q`) using `DATEADD`, the resulting date will be the same day of the month as the input date. If the input date doesn't exist in the resulting month, it will be adjusted accordingly.

To achieve your desired result of the last day of the quarter, you can use the `EOMONTH` function (available in SQL Server 2012 and later) in combination with `DATEADD`. Here's an example:

DECLARE @inputDate DATE = '1997-09-30';

DECLARE @endOfQuarter DATE;

-- Add one quarter to the input date

SET @endOfQuarter = DATEADD(q, 1, @inputDate);

-- Get the end of the resulting month

SET @endOfQuarter = EOMONTH(@endOfQuarter);

SELECT @endOfQuarter;

In this example, the `DATEADD` function is used to add one quarter to the input date. Then, the `EOMONTH` function is used to obtain the end of the resulting month, which will be the last day of the quarter.

The result of the above query will be:

1997-12-31

By using `EOMONTH` to get the end of the month, you ensure that you always get the last day of the resulting month, which corresponds to the last day of the quarter in this case.

Please note that the `EOMONTH` function may not be available in earlier versions of SQL Server. If you're using an older version, you can explore alternative approaches, such as using a combination of `DATEADD` and `DATEPART` functions to calculate the end of the quarter.
0 votes
by (7.8k points)
The behavior you are observing with the `DATEADD` function is actually the expected behavior in SQL Server. When adding quarters to a date using `DATEADD`, the resulting date will always be the same day of the month as the input date, unless the resulting month doesn't have that day. In that case, it will roll back to the last day of the resulting month.

To achieve your desired result of getting the quarter end date, you can use the following approach:

SELECT DATEADD(DAY, -1, DATEADD(QUARTER, 1, '1997-09-01'))

In this example, we first add one quarter to the input date using `DATEADD`. Then we subtract one day from the resulting date using `DATEADD` again, with the `DAY` parameter set to -1. This will give you the quarter end date.

For the specific date you mentioned, '1997-09-30', you can use the following query:

SELECT DATEADD(DAY, -1, DATEADD(QUARTER, 1, DATEADD(DAY, 1, '1997-09-30')))

Here, we first add one day to '1997-09-30' using `DATEADD`. Then we add one quarter to the resulting date using `DATEADD`. Finally, we subtract one day from the resulting date using `DATEADD` with the `DAY` parameter set to -1.

Both queries will give you the expected result of '1997-12-31 00:00:00.000' for adding one quarter to '1997-09-30'.

Browse Categories

...