Calculate a date 10 days after a specific date

Sofie September 28, 2020

Hi,

I have already the date difference between 'Start date' and 'End date' but I also want to calculate the date 10 days after the "End date" in a table.

The first line is OK but when I add the second one the result in column 'End date plus 2 weeks' says Invalid date. It also duplicates the rows. 

SELECT *, DATEDIFF(day,'Start date','End date')+ " days" as 'Duration' from T1 UNION
SELECT *, DATE_ADD(DAY, 10, "End date") AS 'End date plus 2 weeks' from T1

How can I fix this?

 

Thanks

Sofie2020-09-28_9-04-05.jpg

 

1 answer

1 vote
Andrey Khaneev _StiltSoft_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 28, 2020

Hi Sofie,

Please, try this query:

SELECT *, DATEDIFF(day,'End date','Start date')+ " days" as 'Duration', DATE_ADD(DAY, 10, 'End date') AS 'End date plus 2 weeks' from T1
Sofie September 28, 2020

Hi,

Thank you for your answer. Almost there ...

The duplicated row disappeared, but now 'Duration' is negative date and 'End date plus 2 weeks' still invalid Date.

I'm a complete beginner so really appreciate your help !

2020-09-28_9-28-07.jpg

Andrey Khaneev _StiltSoft_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 28, 2020

I reproduced your usw case and this query works for me:

SELECT *, DATEDIFF(day,'Start date','End date')+ " days" as 'Duration',
FORMATDATE('End date' + "240h") AS 'End date plus 2 weeks' FROM T1

 Also, you can use "10d" instead of "240h", but make sure 1 day is set to 24 hours in the settings (by default it is set to 8 working hours).

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events