How to Add or Subtract N Days to The Dates in a Column in Sheets Query

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:

AB
1DescriptionPurchase Date
2Hosting 127/01/2019
3Domain Registration 127/01/2019
4Hosting 214/06/2019
5Domain 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'")
Add n days to the dates in a column in Query

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.

The DATEVALUE function returns header row error

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")
Subtracting 30 days from a date column in Query

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.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Hierarchical Numbering Sequences in Excel

Creating hierarchical numbering sequences in an Excel spreadsheet can significantly improve the way you...

How to Easily Repeat a Sequence of Numbers in Excel

Excel offers multiple ways to accomplish tasks, and the simplicity of each approach depends...

Create a Sequence of Dates at Every Nth Row in Excel (Dynamic Array)

Would you like to create a sequence of dates in every nth row in...

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

4 COMMENTS

  1. 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.

  2. 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,

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.