Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-12-01 · container image postgres:17
The message quotes the exact value that failed: invalid input syntax for type date: "". An empty pair of quotes means an empty string, not NULL.
In INSERT … SELECT and ALTER COLUMN … TYPE date, one bad value fails the whole statement, so no rows are converted. A literal in a VALUES list also gets a LINE 1: pointer to the value.
01Empty string from a CSV staging table cast to date
A staging table keeps every CSV column as text. Lee's joined_on is '', and ''::date is not allowed, so neither row is inserted.
Setup
CREATE TABLE members (id int PRIMARY KEY, name text, joined_on date);
CREATE TABLE members_csv (id int, name text, joined_on text);
INSERT INTO members_csv VALUES (1, 'Kim', '2026-11-29'), (2, 'Lee', '');CREATE TABLE
CREATE TABLE
INSERT 0 2INSERT INTO members SELECT id, name, joined_on::date FROM members_csv;ERROR: 22007: invalid input syntax for type date: ""SELECT id, quote_literal(joined_on) AS raw FROM members_csv WHERE joined_on !~ '^\d{4}-\d{2}-\d{2}$'; id | raw
----+-----
2 | ''
(1 row)Find values that are not YYYY-MM-DD with a regex, then cast NULLIF(joined_on, '')::date. Both rows are inserted and Lee's date is NULL.
INSERT INTO members SELECT id, name, NULLIF(joined_on, '')::date FROM members_csv;
SELECT * FROM members ORDER BY id;INSERT 0 2
id | name | joined_on
----+------+------------
1 | Kim | 2026-11-29
2 | Lee |
(2 rows)02Placeholder text like 'N/A' when changing a text column to date
ALTER TABLE contracts ALTER COLUMN end_date TYPE date USING end_date::date stops on row 2, whose value is 'N/A'. The column stays text.
Setup
CREATE TABLE contracts (id int PRIMARY KEY, end_date text);
INSERT INTO contracts VALUES (1, '2026-12-31'), (2, 'N/A'), (3, '2027-03-31');CREATE TABLE
INSERT 0 3ALTER TABLE contracts ALTER COLUMN end_date TYPE date USING end_date::date;ERROR: 22007: invalid input syntax for type date: "N/A"SELECT id, end_date FROM contracts WHERE end_date !~ '^\d{4}-\d{2}-\d{2}$'; id | end_date
----+----------
2 | N/A
(1 row)Map the placeholder to NULL inside USING: USING NULLIF(end_date, 'N/A')::date. The other dates are kept.
ALTER TABLE contracts ALTER COLUMN end_date TYPE date USING NULLIF(end_date, 'N/A')::date;
SELECT * FROM contracts ORDER BY id;ALTER TABLE
id | end_date
----+------------
1 | 2026-12-31
2 |
3 | 2027-03-31
(3 rows)03Localized date text such as '2026년 11월 30일'
PostgreSQL's date input does not understand words like 년/월/일 (year/month/day in Korean). The same happens with any text format it does not know.
Setup
CREATE TABLE orders (id int PRIMARY KEY, order_date date);CREATE TABLEINSERT INTO orders VALUES (1, '2026년 11월 30일');ERROR: 22007: invalid input syntax for type date: "2026년 11월 30일"
LINE 1: INSERT INTO orders VALUES (1, '2026년 11월 30일');
^SELECT to_date('2026년 11월 30일', 'YYYY"년" MM"월" DD"일"') AS converted; converted
------------
2026-11-30
(1 row)Describe the format with to_date, putting literal words in double quotes: to_date('2026년 11월 30일', 'YYYY"년" MM"월" DD"일"') returns 2026-11-30.
INSERT INTO orders VALUES (1, to_date('2026년 11월 30일', 'YYYY"년" MM"월" DD"일"'));
SELECT * FROM orders;INSERT 0 1
id | order_date
----+------------
1 | 2026-11-30
(1 row)04Empty "from date" in a search form: an empty string is cast to timestamp
The order search query takes the start date from the form as a text parameter and compares created_at >= $1::timestamp. With '2026-12-01' it returns order 2, but when the user leaves the field blank and '' arrives, it fails with invalid input syntax for type timestamp: "".
Empty quotes in the message mean an empty string, not NULL, was passed. Here the error came without a LINE 1: marker because the value was a parameter of a prepared statement.
Setup
CREATE TABLE orders (id int PRIMARY KEY, created_at timestamp NOT NULL);
INSERT INTO orders VALUES (1, '2026-11-20 09:00'), (2, '2026-12-01 14:30');CREATE TABLE
INSERT 0 2PREPARE find_orders(text) AS
SELECT id, created_at FROM orders
WHERE created_at >= $1::timestamp
ORDER BY id;
EXECUTE find_orders('2026-12-01');
EXECUTE find_orders('');PREPARE
id | created_at
----+---------------------
2 | 2026-12-01 14:30:00
(1 row)
ERROR: 22007: invalid input syntax for type timestamp: ""SELECT quote_literal('') AS raw, NULLIF('', '') IS NULL AS becomes_null; raw | becomes_null
-----+--------------
'' | t
(1 row)First we checked that NULLIF('', '') gives NULL (becomes_null is t). Then NULLIF($1, '') turns the empty string into NULL, and a NULL skips the condition. In the retry '2026-12-01' returned 1 row and '' returned both rows.
PREPARE find_orders(text) AS
SELECT id, created_at FROM orders
WHERE NULLIF($1, '') IS NULL
OR created_at >= NULLIF($1, '')::timestamp
ORDER BY id;
EXECUTE find_orders('2026-12-01');
EXECUTE find_orders('');PREPARE
id | created_at
----+---------------------
2 | 2026-12-01 14:30:00
(1 row)
id | created_at
----+---------------------
1 | 2026-11-20 09:00:00
2 | 2026-12-01 14:30:00
(2 rows)FAQ
How do I fix invalid input syntax for type date in PostgreSQL?
Look at the quoted value in the message. Turn empty strings or placeholders into NULL with NULLIF, and use to_date with a format for non-standard text.
How do I cast an empty string to date in PostgreSQL?
You cannot cast '' directly. Use NULLIF(col, '')::date so empty strings become NULL.
Why do I get invalid input syntax for type timestamp: ""?
The value being cast to timestamp is an empty string. In this page it happened when the search form's start date was blank and '' was cast with $1::timestamp. Converting it with NULLIF($1, '') and skipping the condition when it is NULL fixed it.
ALTER COLUMN TYPE date fails with invalid input syntax, what should I do?
Find the rows that do not look like dates, then handle them in the USING clause, for example USING NULLIF(col, 'N/A')::date.
What is the difference between 22007 and 22008?
22007 means the text is not a date at all. 22008 means it looks like a date but a field is out of range, such as month 13.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12