HomeGoogle DocsSpreadsheetHow to Use DateTime in Query in Google Sheets

How to Use DateTime in Query in Google Sheets

Published on

A Google Apps Script, function, or external application can fill cells in Google Docs Sheets with DateTime (timestamp). My topic is not about inserting timestamps. Let’s learn how to handle DateTime in Query in Google Sheets.

The custom function onEdit and the built-in NOW() are the two functions that insert DateTime in Google Sheets.

Also, If you have connected Google Docs Forms with Google Sheets, you can see that the first column in your connected Spreadsheet is filled with timestamp/DateTime values.

Here, we will learn how to use the DateTime keyword in Google Sheets Query. I mean, filter, group, and format a timestamp column.

How to Use DateTime in Query Where Clause in Google Sheets

Before going to learn the use of DateTime in the Query Where clause, you must know about the Query language element called Literals.

Literals are values used for comparisons/assignments.

For date/time types of literals, there are three keywords: date, timeofday, and datetime or timestamp.

That means, to compare a column that contains date and time, you can use the keyword datetime or timestamp as below.

Examples of the Use of DateTime in Query in Google Sheets:

Formula when column A contains timestamp/DateTime.

=query(A1:D,"select A,B,C,D where A>= timestamp '2019-1-11 12:00:00'",1)

In the above filter, column A contains the timestamp/DateTime. Below is an alternative Query.

=query(A1:D,"select A,B,C,D where A>= datetime '2019-1-11 12:00:00'",1)

Both formulas would return the same result.

DateTime in Query in Google Sheets

DateTime Criterion as Cell Reference in Query

Please refer to cell A2 in the above image. How to use that value as the criterion?

The following example shows how to specify a timestamp as a cell reference in the Where clause in Google Sheets Query.

DateTime Criterion as Cell Reference:

=query(A1:D,"select A,B,C,D where A>= datetime '"&TEXT(A1,"yyyy-mm-dd HH:mm:ss")&"'",1)

Again, you can replace the keyword datetime in the formula with the timestamp keyword.

How to Format DateTime in Query Output

When you format a DateTime column using the Query format clause, it won’t affect the actual DateTime, or we can say, the underlying value in the cell.

It helps us to display the timestamp in date format while keeping the value intact.

Similar: How to Format Date, Time, and Number in Google Sheets Query.

The Use of DateTime in Format Clause:

=query(A1:D,"select A,B,C,D where A>= datetime '"&TEXT(F1,"yyyy-mm-dd HH:mm:ss")&"' format A 'DD-MM-YY'",1)
Format DateTime in Sheets Query

See the value on the formula bar. It is not a date in DD-MM-YY as displayed in column F. It contains time values.

This formula masks the time elements with formatting. Again the underlying value in the cells will be the same.

=query(A1:D,"select A,B,C,D where A>= datetime '"&TEXT(F1,"yyyy-mm-dd HH:mm:ss")&"' format A 'DD-MM-YY HH:MM'",1)

DateTime in Query Group By Clause in Google Sheets

For this example, I am not again posting the screenshot. See the columns A, B, C, and D in the first screenshot of this tutorial.

In that, the following formula will group the data based on DateTime in column A and then by the Product in column B.

=query(A1:D,"select A,B, Sum(C) where A is not null group by A,B")
Group DateTime (timestamp) in Query in Sheets

Take a look at the DateTime in column F in the above result. It won’t be the output that you were expecting.

Because it doesn’t group by dates. So, you may want to exclude the time value in the grouping.

The solution is to use the toDate scalar function both in the Select and Where clauses as below.

=query(A1:D,"select todate(A),B, Sum(C) where A is not null group by todate(A),B")

That’s all. Thanks for the stay. Enjoy!

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.

Creating Custom Descending Sequence Lists in Excel

You can utilize functions like ROW or SEQUENCE in Excel to create a custom...

SUMIFS with XLOOKUP in Excel and Google Sheets

SUMIFS and SUMIF are infamous for producing an "argument must be a range" error...

Counting XLOOKUP Results with COUNTIFS in Excel and Google Sheets

We can use COUNTIF or COUNTIFS alongside an XLOOKUP formula to conditionally count the...

Appointment Schedule Template in Google Sheets

An appointment schedule template in Google Sheets can assist you in efficiently managing your...

More like this

Appointment Schedule Template in Google Sheets

An appointment schedule template in Google Sheets can assist you in efficiently managing your...

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

5 COMMENTS

  1. Hi,

    I am writing a function to extract date, where my Col1 consists of date and time and my Col2 consists of date only.

    =query({Sheet4!A1:Q},"select Col1, Col2 where Col1 = date '"&TEXT(today(),"yyyy-mm-dd")&"' or Col2 = date '"&TEXT(today(),"yyyy-mm-dd")&"' ")

    But i am facing issue to extract Col1 properly as it misses out some and only extract some of the cells with today’s date.

  2. Hi Prashanth,

    I am running a query from another sheet where the column is formatted as “Date Time” but the query tab only the date part is displayed.

    Example:

    =Query ({Sheet1!A:D}, "select Col1,Col2,Col3",1)

    Column A in the source sheet is formated as “Date Time” but in the destination Column, it is displayed as Date only, even when I change its format to “Date Time”, the hour, minute and second part are displayed as zeros. Any reason for that?

    Thanks

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.