Skip to main content
Solved

Writing nulls into numeric field from csv

  • March 24, 2016
  • 7 replies
  • 93 views

Forum|alt.badge.img

Reading a csv file with null entries

ID, A, B, C, D

QA123, 1,2,3,4

QA456,,,,

Into a table with character_varying, numeric, numeric, numeric, numeric

Writer fails on line QA456 with error

Error was 'ERROR: invalid input syntax for type numeric: ""

CONTEXT: COPY table, line 2, column A: ""

How can I deal with this issue?

Thanks

Using FME Desktop 2015.1

Best answer by david_r

The problem is that the CSV reader maps the empty values to empty strings, and not to NULL.

You can easily solve this using an NullAttributeMapper, like this:

Your empty string values will now be mapped to NULL, which should render them acceptable to the database.

David

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

7 replies

erik_jan
Contributor
Forum|alt.badge.img+23
  • Contributor
  • March 24, 2016

In line 2 the value for A contains a space " 1".

That might cause the issue.

You can change the reader to read the csv as char fields.

Then use the AttributeTrimmer to get rid of the unwanted spaces and write to a Numeric output.


Forum|alt.badge.img

Thank you, it's line 2 (after the header) that's the problem.

QA456,,,,


erik_jan
Contributor
Forum|alt.badge.img+23
  • Contributor
  • March 24, 2016

Same solution.

Change the input data type to char as in numeric fields the comma can be a separator.

But as far as I know the line 2 in the error message is the second line in the CSV (including the line to skip).


Forum|alt.badge.img

For more information to remove confusion over which line.


this is the Table creation:


CREATE TABLE andy.test
(id character varying NOT NULL,a numeric,b numeric,c numeric,d numeric);


This is the file:


ID,A,B,C,D
QA123,1,2,3,4
QA456,,,,


The copied error from the log:

Bulk copy failed on table 'andy.test' using delimiter ':'. Error was 'ERROR:  invalid input syntax for type numeric: ""
CONTEXT:  COPY test, line 2, column a: ""


The first row (QA123) is inserted correctly if I do a commit after every record.


Thanks


Andy


david_r
Celebrity
  • Best Answer
  • March 24, 2016

The problem is that the CSV reader maps the empty values to empty strings, and not to NULL.

You can easily solve this using an NullAttributeMapper, like this:

Your empty string values will now be mapped to NULL, which should render them acceptable to the database.

David


erik_jan
Contributor
Forum|alt.badge.img+23
  • Contributor
  • March 24, 2016

Aha, the issue is not the CSV reader but the writer trying insert non_numeric data into a numeric table field.

Have you tried the AttributeTrimmer before writing (with the setting to allow attributes to be trimmed to nothing set to Yes)?


Forum|alt.badge.img

The problem is that the CSV reader maps the empty values to empty strings, and not to NULL.

You can easily solve this using an NullAttributeMapper, like this:

Your empty string values will now be mapped to NULL, which should render them acceptable to the database.

David

BINGO! Thanks