To filter or format dates with the QUERY function in Google Sheets, you write a SQL-like query string that references your date column by letter (for example, Col1) and compares it to a date literal written as date '2024-01-15'. This guide shows you the exact syntax, how to handle dates that are stored as text, and how to fix the most common errors.
What You'll Need
- A Google account with access to Google Sheets.
- A spreadsheet containing at least one column of dates. The dates can be real date values or text that looks like a date, but the two behave differently (explained below).
- Basic familiarity with cell references and the QUERY function's three arguments: data, query, and headers.
Step-by-Step
- Confirm your dates are real dates. Select the date column and check the format. Real dates are right-aligned by default and can be formatted as dates. If they are left-aligned, they are probably text. You can convert text to dates with
DATEVALUEin a helper column, or by using the Format menu to apply a date format after re-entering the values. The QUERY function only treats a column as a date if the underlying values are date serial numbers. - Identify the column letter. In QUERY, columns are referenced as Col1, Col2, and so on, based on their position in the data range you pass, not the sheet's column letters. If your data range starts at A1 and the date column is column C, it is Col3.
- Write the basic query. In an empty cell, type
=QUERY(A1:C100, "SELECT * WHERE Col3 > date '2024-01-01'", 1). The third argument (1) tells QUERY that the first row of the range contains headers. Adjust the range, column number, and date to match your sheet. - Use the correct date literal format. Dates in the query string must be written as
date 'YYYY-MM-DD'. For example,date '2024-03-15'. Do not use slashes or a local date format inside the query string; QUERY expects the ISO format. - Filter a range of dates. Combine two conditions with
ANDorOR:"SELECT * WHERE Col3 >= date '2024-01-01' AND Col3 <= date '2024-01-31'". This returns all rows in January 2024. - Sort and limit results. Add
ORDER BY Col3 DESCto sort newest first, andLIMIT 10to return only the top ten rows. Place these after the WHERE clause:"SELECT * WHERE Col3 > date '2024-01-01' ORDER BY Col3 DESC LIMIT 10". - Reference a date from another cell. QUERY does not accept cell references directly in the query string. Instead, build the string with concatenation:
"SELECT * WHERE Col3 > date '" & TEXT(A1, "yyyy-mm-dd") & "'". Here A1 holds the cutoff date, and TEXT converts it to the required format. - Handle dates stored as text. If your date column is text, QUERY's date comparisons will not work. Convert the column to real dates first, or use string comparisons only if the text is in ISO format (YYYY-MM-DD), which sorts correctly as text. For reliable results, convert to real dates.
- Test and adjust. If you get an error, check the column number, the date format, and whether the range includes the header row. The third argument should be 1 if the first row is a header, or 0 if not.
Common Mistakes
- Using slashes in the date literal.
date '2024/01/15'will cause an error. Use hyphens:date '2024-01-15'. - Referencing the wrong column number. Col1 is the first column in your data range, not column A of the sheet, unless your range starts at A.
- Forgetting the header argument. If your data has a header row and you omit the third argument, QUERY may treat the header as data and return unexpected results.
- Assuming text dates work like real dates. QUERY's date functions only work on real date values. Text that looks like a date will not compare correctly.
- Using cell references inside the query string without concatenation. The query string is plain text; you must concatenate cell values and format them properly.
Tips
- Use
TEXT(A1, "yyyy-mm-dd")to convert a date cell to the format QUERY expects when building dynamic queries. - To filter by today's date, use
TODAY()inside the TEXT function:"SELECT * WHERE Col3 = date '" & TEXT(TODAY(), "yyyy-mm-dd") & "'". - If you need to filter by month or year, use
YEAR(Col3) = 2024orMONTH(Col3) = 1in the WHERE clause, but note that these functions may not work on text dates. - Keep your data range as small as practical to improve performance, especially with large sheets.
- Use
FORMAT Col3 'yyyy-mm-dd'in the SELECT clause to display dates in a specific format in the results.
Troubleshooting
If your QUERY returns an error like "Unable to parse query string," check the date literal format and ensure all quotes are balanced. If you get a #VALUE! error, confirm that the column number exists in your range. If results are empty when you expect data, verify that the date column contains real dates and that your comparison operators are correct. If the query works but returns the header row as data, set the third argument to 1. If dates appear as numbers in the results, format the output column as a date. For dynamic queries that use cell references, make sure the concatenation produces a valid date string with hyphens and no extra spaces.
Frequently Asked Questions
Can I use a cell reference in a QUERY date condition?
Not directly. You must concatenate the cell value into the query string, using TEXT to format it as yyyy-mm-dd.
Why does my QUERY return no results when I know the dates match?
The most common cause is that the date column is stored as text, not as real dates. Convert the column to date values and try again.
How do I filter for a specific month or year?
Use the YEAR() and MONTH() functions in the WHERE clause, for example WHERE YEAR(Col3) = 2024. These work only on real date values.
Can I sort by date in the results?
Yes. Add ORDER BY Col3 (ascending) or ORDER BY Col3 DESC (descending) after the WHERE clause.
What date format does QUERY expect?
QUERY expects dates in the literal format date 'YYYY-MM-DD', with hyphens and no time component.
Does QUERY work with dates that include times?
Yes, but the time portion is considered. To compare only dates, you may need to use DATE(Col3) or ensure your comparison uses the full datetime value.