BigQuery error · Data and type conversion
Could not cast literal to type
A string literal in your SQL is compared with or passed to a DATE, DATETIME, TIME or TIMESTAMP, and it isn't a valid value of that type, such as "2024-02-30" or "01/15/2024". BigQuery checks this when it analyzes the query, so nothing runs. Fix the literal, or parse it with PARSE_DATE and an explicit format.
The error message
Could not cast literal "<value>" to type <TYPE> at [<line>:<column>]
Causes
A non-ISO or impossible date in a filter
BigQuery coerces a string literal to the column's type. WHERE order_date = "01/15/2024" fails with Could not cast literal "01/15/2024" to type DATE; so do impossible dates like "2024-02-30" and "2024-13-01". Dates must be YYYY-MM-DD.
A time part on a DATE bound
A common BETWEEN pattern order_date BETWEEN "2024-01-01" AND "2024-01-31 23:59:59" fails on a DATE column, because a DATE literal can't carry a time.
Invalid TIMESTAMP literals
Hours past 23 or missing parts fail the same way, for example Could not cast literal "2024-01-01 25:00:00" to type TIMESTAMP.
Typed literals and parameters
A typed literal such as DATE "2024-02-30" fails with Invalid DATE literal at [line:column] instead. Values injected by BI tools or templating (Looker, Jinja) into SQL text cause most of these errors.
How to fix it
1. Write the literal in canonical form
Use YYYY-MM-DD for DATE and YYYY-MM-DD HH:MM:SS for DATETIME and TIMESTAMP. For a DATE range, use whole days.
WITH orders AS (
SELECT DATE '2024-01-15' AS order_date, 120 AS amount
)
SELECT SUM(amount) AS january_revenue
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-01-31';2. Parse non-standard formats explicitly
When a value arrives as 15/01/2024, convert it with the matching format instead of relying on coercion.
SELECT PARSE_DATE('%d/%m/%Y', '15/01/2024') AS order_date;3. Use typed query parameters
Pass dates from applications as DATE or TIMESTAMP query parameters (@start_date) instead of formatting them into the SQL text. The client validates the value before the query is sent.
4. Catch it for free with a dry run
Because the check happens during analysis, a dry run (the query validator in the console, or bq query --dry_run) shows the error without running anything.
How to prevent it
- Build date filters with functions (
DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)) instead of string literals where you can. - Validate user-supplied dates in the application before they reach SQL.
- The query never starts, so nothing is billed and no slots are used.
FAQ
Why does CAST('abc' AS INT64) not give this error?
This error comes from implicit literal coercion, which BigQuery applies only from string literals to DATE, DATETIME, TIME and TIMESTAMP. An explicit CAST of a string to a number is evaluated when the query runs and fails with Bad int64 value.
Does SAFE_CAST help?
Not for a literal in a comparison, because the coercion happens before execution. Use SAFE_CAST or SAFE.PARSE_DATE on column values that may be invalid at run time.
What does the [line:column] part mean?
It is the position of the literal in the SQL text: line 1, column 17 means the 17th character of the first line, which helps find the value in a long generated query.
Related
SQL reference
More data and type conversion
All BigQuery error messages · What 50M daily events say about BigQuery failures
