I’m starting to learn sql in porstgresql. I have create a table with numeric(6,2) structure and trying to import data. But data has some row that has NULL value. Now I’m getting "ERROR: invalid input syntax for type numeric: NULL"
Now I have at least 20 column that contain that NULL value.
What should I do in that situation? Should I alter all the column manually or is it possible to do this automatically for all the column?
CREATE TABLE ireland_income_gap_bonus (
id_c smallint,
companyName text, companies_ID smallint,
meanBonus numeric(6,2),
meanHourly numeric(6,2),
medianBonus numeric(6,2),
medianHourly numeric(6,2),
reportLink text,
year_ smallint,
meanHourlyPT numeric(6,2),
medianHourlyPT numeric(6,2),
meanHourlyTemp numeric(6,2),
medianHourlyTemp numeric(6,2),
perBonusFemale numeric(6,2),
perBonusMale numeric(6,2),
perBIKFemale numeric(6,2),
perBIKMale numeric(6,2),
pb1Female numeric(6,2),
pb1Male numeric(6,2),
pb2Female numeric(6,2),
pb2Male numeric(6,2),
pb3Female numeric(6,2),
pb3Male numeric(6,2),
pb4Female numeric(6,2),
pb4Male numeric(6,2),
perEmployeesFemale numeric(6,2),
perEmployeesMale numeric(6,2),
commentss text
)
Then,
COPY ireland_income_gap_bonus
FROM 'E:ProgrammingSQLProjectDataIreland_gender_pays_gapIreland_gpg.CSV'
WITH (FORMAT CSV, HEADER)
Return messages,
ERROR: invalid input syntax for type numeric: "NULL" CONTEXT: COPY
ireland_income_gap_bonus, line 2, column meanhourlypt: "NULL"SQL state: 22P02
2
Answers
Using
psql
and it’sCOPY
meta-commandcopy
and theNULL
parameter documented here COPY:The issue with this is it assumes that all
NULL
values are represented by'null'
. Ifnull
is also represented byNULL
or the unquoted empty string or some other value they will not be converted on import as in:To treat the string
NULL
in the csv as null value, use theNULL 'null_string'
option inCOPY
(documented here):