Мне показалось, что я очень хорошо знаю PostgreSQL и уже много раз делал это раньше, но у меня есть
\copy MyTable FROM ... WITH CSV NULL AS ''
и, несмотря на это, такой ряд, как:
"FooBar","","","1","0","","29505;29505",""
второй столбец будет импортирован не как NULL, а как пустая строка!
SELECT count(1) FROM MyTable WHERE col2 IS NULL
== 0
SELECT count(1) FROM MyTable WHERE col2 = ''
>= 1
Что я делаю не так? Такая простая вещь!
Вот полностью самодостаточный тест, чтобы доказать это:
CREATE TABLE Test(col1 text, col2 int);
\copy Test FROM PROGRAM 'printf '',\na,2\n''' WITH DELIMITER ',' NULL ''
SELECT (SELECT count(1) FROM Test WHERE col1 IS NULL) AS col1null,
(SELECT count(1) FROM Test WHERE col2 IS NULL) AS col2null,
(SELECT count(1) FROM Test WHERE col1 = '') AS col1empty;
TRUNCATE Test;
\copy Test FROM PROGRAM 'printf ''"",""\n"a","2"\n''' WITH CSV NULL ''
\copy Test FROM PROGRAM 'printf ''"","0"\n"a","2"\n''' WITH CSV NULL ''
SELECT (SELECT count(1) FROM Test WHERE col1 IS NULL) AS col1null,
(SELECT count(1) FROM Test WHERE col2 IS NULL) AS col2null,
(SELECT count(1) FROM Test WHERE col1 = '') AS col1empty;
DROP TABLE Test;
И вот как он работает:
foo=# CREATE TABLE Test(col1 text, col2 int);
SELECT (SELECT count(1) FROM Test WHERE col1 IS NULL) AS col1null,
(SELECT count(1) FROM Test WHERE col2 IS NULL) AS col2null,
(SELECT count(1) FROM Test WHERE col1 = '') AS col1empty;
TRUNCATE Test;
\copy Test FROM PROGRAM 'printf ''"",""\n"a","2"\n''' WITH CSV NULL ''
\copy Test FROM PROGRAM 'printf ''"","0"\n"a","2"\n''' WITH CSV NULL ''
SELECT (SELECT count(1) FROM Test WHERE col1 IS NULL) AS col1null,
(SELECT count(1) FROM Test WHERE col2 IS NULL) AS col2null,
(SELECT count(1) FROM Test WHERE col1 = '') AS col1empty;
DROP TABLE Test;CREATE TABLE
foo=# \copy Test FROM PROGRAM 'printf '',\na,2\n''' WITH DELIMITER ',' NULL ''
COPY 2
foo=# SELECT (SELECT count(1) FROM Test WHERE col1 IS NULL) AS col1null,
foo-# (SELECT count(1) FROM Test WHERE col2 IS NULL) AS col2null,
foo-# (SELECT count(1) FROM Test WHERE col1 = '') AS col1empty;
col1null | col2null | col1empty
----------+----------+-----------
1 | 1 | 0
(1 row)
foo=# TRUNCATE Test;
TRUNCATE TABLE
foo=# \copy Test FROM PROGRAM 'printf ''"",""\n"a","2"\n''' WITH CSV NULL ''
ERROR: invalid input syntax for integer: ""
CONTEXT: COPY test, line 1, column col2: ""
foo=# \copy Test FROM PROGRAM 'printf ''"","0"\n"a","2"\n''' WITH CSV NULL ''
COPY 2
foo=# SELECT (SELECT count(1) FROM Test WHERE col1 IS NULL) AS col1null,
foo-# (SELECT count(1) FROM Test WHERE col2 IS NULL) AS col2null,
foo-# (SELECT count(1) FROM Test WHERE col1 = '') AS col1empty;
col1null | col2null | col1empty
----------+----------+-----------
0 | 0 | 1
(1 row)
foo=# DROP TABLE Test;
DROP TABLE
Так что я могу сделать, чтобы получить "" CSV, как в текстовых столбцах, интерпретируемых как NULL?