BigQuery error · Data and type conversion
Failed to parse input string
PARSE_DATE, PARSE_DATETIME or PARSE_TIMESTAMP got a string that doesn't match the format you passed, typically extra characters (a time after a date), a missing part, or a T where the format has a space. Match the format to the data exactly, or use SAFE.PARSE_DATE to get NULL for rows that don't fit.
The error message
Failed to parse input string "<value>"
Causes
The string has more than the format describes
PARSE_DATE('%Y-%m-%d', '2024-01-15 10:30:00') fails with Failed to parse input string "2024-01-15 10:30:00": the whole input must be consumed by the format.
The string has less than the format expects
PARSE_DATETIME('%Y-%m-%d %H:%M:%S', '2024-01-15') fails because the time part is missing. Mixed columns where some rows have a time and some don't hit this constantly.
ISO 8601 "T" separators
PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '2024-01-15T10:30:00') fails because the format has a space where the data has T. Use %Y-%m-%dT%H:%M:%S, or %FT%T.
Related messages from the same functions
A wrong separator gives Mismatch between format character '-' and string character '/' at string index: 4 with format: '%Y-%m-%d'. A well-formed but impossible date such as 2024-02-30 gives Out-of-range datetime field in parsing function. CAST(... AS DATE FORMAT ...) reports Failed to parse input timestamp string at 5 with format element 'MM'.
How to fix it
1. Match the format to the data
Every character in the input must be covered by the format: literal separators, T, and fractional seconds (%E*S).
SELECT
PARSE_DATE('%d/%m/%Y', '15/01/2024') AS d,
PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%S', '2024-01-15T10:30:00') AS ts,
PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%E*S', '2024-01-15 10:30:00.123') AS ts_ms;2. Parse only the part you need
To get a date from a timestamp-like string, cut it first or parse the full timestamp and convert.
SELECT
PARSE_DATE('%Y-%m-%d', LEFT('2024-01-15 10:30:00', 10)) AS d1,
DATE(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '2024-01-15 10:30:00')) AS d2;3. Handle several formats with SAFE. and COALESCE
SAFE.PARSE_DATE returns NULL instead of failing, so you can try formats in order and keep unparseable rows visible.
WITH raw AS (
SELECT '2024-01-15' AS s UNION ALL
SELECT '15/01/2024' UNION ALL
SELECT 'January 15'
)
SELECT
s,
COALESCE(
SAFE.PARSE_DATE('%Y-%m-%d', s),
SAFE.PARSE_DATE('%d/%m/%Y', s)
) AS parsed
FROM raw;How to prevent it
- Agree on ISO 8601 with data producers;
YYYY-MM-DDandYYYY-MM-DD HH:MM:SSalso cast directly withCAST(s AS DATE)andCAST(s AS TIMESTAMP). - Count rows where
SAFE.PARSE_...returns NULL but the input isn't NULL, and alert when the share grows. - The error appears at run time, after the scan. On-demand failed queries aren't billed; under capacity pricing the slots used count, and scheduled retries fail until the format is fixed.
FAQ
Why does PARSE_DATE('%Y-%m-%d', '2024-1-5') work?
Numeric elements such as %m and %d accept values without leading zeros when parsing, so 2024-1-5 returns 2024-01-05. Extra or missing characters are what fail.
What is the difference between PARSE_DATE and CAST(... AS DATE)?
CAST accepts only the canonical YYYY-MM-DD form (and fails with Invalid date: '...' otherwise) unless you add a FORMAT clause. PARSE_DATE takes any format you describe and gives more specific errors.
Does the error show which row failed?
It shows the value, not the row. Search for it with WHERE SAFE.PARSE_DATE(fmt, col) IS NULL AND col IS NOT NULL.
Related
SQL reference
More data and type conversion
All BigQuery error messages · What 50M daily events say about BigQuery failures
