The arithmetic operators in Query won’t work in a date or time column. So to add or subtract n days to the dates in a column in Query, you can follow the below two methods.
Assume column B contains dates. I am going to add 30 days to the dates in this column.
Does this Query work?
=Query(A1:C,"Select B+30")
It won’t work! You will get a #VALUE! error saying “can’t perform the function sum on values that are not numbers“.
Let’s try to subtract 30 days with the dates in column B using another Query.
=Query(A1:C,"Select B-30")
Here also you will see a similar error, i.e., “can’t perform the function difference on values that are not numbers“.
Related: Group and Sum Time Duration Using Google Sheets Query.
Then how to add or subtract n days to the dates in a column in Query? Is there any equivalent aggregation function available in the SQL Query in Sheets?
No! There are no such functions available. You can solve this issue using a workaround as below.
Two Methods to Add or Subtract N Days to The Dates in a Column in Query
Let me start with adding dates using method 1:
Adding 180 Days to a Date Column in Query
Method 1:
A | B | |
1 | Description | Purchase Date |
2 | Hosting 1 | 27/01/2019 |
3 | Domain Registration 1 | 27/01/2019 |
4 | Hosting 2 | 14/06/2019 |
5 | Domain Registration 2 | 14/06/2019 |
I want to add 180 days to the dates in column B. Also I want to filter column A containing the string “Hosting”.
As you may know by now, the arithmetic operator in SQL Query in Sheets works in a numeric column.
Dates are actually numbers in Google Sheets. So the arithmetic operator ‘Addition’ will work if you convert the dates in column B to numbers aka date values. How?
Select the dates in column B and go to the menu Format and select Numbers > Numbers. Then use the below formula.
=Query(A1:B,"Select A,B,B+180 where A contains 'Hosting' label B+180 'Expiry'")
The Query is in cell D1. You must format the range E2:F4 to date from Format > Number > Date.
Method 2:
In the second method no need to format the dates in column B to number. That we can do within Query data as below.
=ArrayFormula(Query({A1:A,datevalue(B1:B)},"Select Col1,Col2,Col2+180 where Col1 contains 'Hosting' label Col2+180 'Expiry'"))
I have used the DATEVALUE function within Query data. Since the header row includes in the DATEVALUE formula, it would return #VALUE! error as below.
That you can solve using labeling in Query.
=ArrayFormula(Query({A1:A,datevalue(B1:B)},"Select Col1,Col2,Col2+180 where Col1 contains 'Hosting' label Col2+180 'Expiry',Col2 'Join Date'"))
Subtracting 30 Days from a Date Column in Query
I think you can guess the formula now! Simply change +
operator in the formula to -
. In the below examples, I am subtracting 30 days from the dates in column A.
If you use the method 1 above, first you need to change the date format to number. Once done, you can use this Query.
=Query(A1:A,"Select A, A-30")
The output in column C and D will be in numbers. Do format it to date.
If you follow method 2, use this formula. Here you only format the output columns to date.
=ArrayFormula(Query(datevalue(A1:A),"Select Col1, Col1-30 label Col1 'Date', Col1-30 'Date-30'"))
This way you can add or subtract n days to the dates in a column in Query in Sheets.
Thanks, I got the output for the above ask.
Thanks, Prashanth,
How do we change the date by adding time to it?
Example: The Planned start time is 23:50:00 PM and Planned end time is 3:20:00 AM
The Planned start date is 8/7/2019 and how to get the planned output basis on 3 conditions?
I need output as next day date (8/8/2019) after adding start and end time to the current date.
Hi,
How to add or subtract 1 hour when between two times exceed 9 hours?
Example: If we add 9 hours to start time 10:00:00 AM then the output should roundup to 8 hours only (18:00:00)
Hi, Hima,
Assume the following values are present in your Sheet.
Cell B2: 10:00:00 – This is in time format
Cell C2: 10 – this is hours as a number.
Formula to be used in cell D2 to conditionally add hours to time.
=if(C2>8,B2+time(8,0,0),B2+time(C2,0,0))
Best,