Examples of the Use of Literals in Query in Google Sheets

This article dives into the world of Query formulas in Google Sheets, specifically focusing on the use of different literals. Whether you’re a beginner or an experienced user, understanding how to utilize literals can greatly enhance your querying capabilities. So let’s explore these examples together and unlock the full potential of Query in Google Sheets.

Literals in Google Sheets Query (Formula Examples)

Before we dive into the specifics, it’s important to note that this article assumes some familiarity with Query formulas. We won’t be covering the basics, but rather focusing on the use of literals within these formulas. So if you’re new to Query, it might be helpful to brush up on the basics before proceeding.

Now, let’s jump right into it and explore the different types of literals we can use in Query formulas.

1. String Literals in Sheets Query

Strings play a crucial role in Query formulas, and understanding how to use string literals is essential. In Query, string literals are case-sensitive and must be enclosed within single or double quotes.

Sample data (data type is "text")

Here are a few formula examples that demonstrate the use of string literals within single and double quotes:

  • Using a string literal directly in the formula:

    =query(A1:D, "SELECT * WHERE C='Dubai'", 1)
  • Using a string literal as a cell reference:

    =query(A1:D, "SELECT * WHERE C='"&F1&"'", 1)

To escape single quotes (apostrophes) within a string literal, you can use double quotes like so:

=query(A1:D, "SELECT * WHERE D=""Nature's Wisdom""", 1)

And if you need to use a string literal as a cell reference while escaping single quotes:

=query(A1:D, "SELECT * WHERE D="""&F1&"""", 1)

2. Number Literals in Sheets Query

While the provided sample data doesn’t include a numeric field, we can still explore number literals in Query formulas. Number literals are straightforward and can be used in various scenarios.

For example, to limit the number of results to 5:

=query(A1:D, "SELECT * LIMIT 5", 1)

If we have a two-column dataset in range A1:B, and column B contains numbers, we can filter based on a number literal like this:

=query(A1:B, "SELECT * WHERE B=4", 1)

Using a number literal as a cell reference:

=query(A1:B, "SELECT * WHERE B="&F1&"", 1)

It’s important to note that number literals should be specified in decimal notation (e.g., 5, 4.5, 3.0, -5.25, etc.).

3. Different Date, DateTime, and Timeofday Literals in Query

Dates, date-time, and timeofday literals are essential for time-based queries. Let’s explore how we can use these literals effectively:

Date Literals

In Query, dates must be specified in the format YYYY-MM-DD. Here’s an example that demonstrates the use of a date literal:

=Query(A1:B, "SELECT * WHERE B <= DATE '1999-12-31'", 1)

To reference a date criterion from a cell (e.g., F1):

=Query(A1:B, "SELECT * WHERE B <= DATE '"&TEXT(F1, "yyyy-mm-dd")&"'", 1)

DateTime (Timestamp) Literals

When working with date-time literals, you can use either the “timestamp” or “datetime” keywords. Here’s an example using the “timestamp” keyword:

=query(A1:B, "SELECT * WHERE B >= TIMESTAMP '2019-5-9 10:00:00'", 1)

Alternatively, you can use the “datetime” keyword:

=query(A1:B, "SELECT * WHERE B >= DATETIME '2019-5-9 10:00:00'", 1)

To reference a date-time criterion from a cell (e.g., F1):

=query(A1:B, "SELECT * WHERE B >= DATETIME '"&TEXT(F1, "yyyy-mm-dd HH:mm:ss")&"'", 1)

Timeofday Literals in Query

If you’re working with time-only data, you can use timeofday literals in your Query formulas. Here’s an example:

=query(A1:B, "SELECT * WHERE B >= TIMEOFDAY '10:00:00'", 1)

To reference a time (criterion) from a cell (e.g., F1):

=query(A1:B, "SELECT * WHERE B >= TIMEOFDAY '"&TEXT(F1, "HH:mm:ss")&"'", 1)

4. The Use of Boolean Literals TRUE or FALSE in Query

Lastly, let’s explore the use of Boolean literals in Query formulas. Boolean literals can be either TRUE or FALSE and can be used in a similar manner as number literals.

Here’s an example that selects records where the value in column B is TRUE:

=query(A1:B, "SELECT * WHERE B=TRUE", 1)

To reference a Boolean criterion from a cell (e.g., F1):

=query(A1:B, "SELECT * WHERE B="&F1&"", 1)

And there you have it! These formula examples demonstrate the effective use of literals in Query formulas. By leveraging these techniques, you can unlock the full potential of Google Sheets and take your data analysis to new heights.

Remember, if you want to dive deeper into the world of Query in Google Sheets, be sure to check out Crawlan.com. They provide valuable insights and resources to help you master the art of Google Sheets Query.

So go ahead, experiment with these literals, and enjoy the power of Query formulas in Google Sheets!

Related posts