6

I am using H2 DB in my java application. I want to load a .csv file to the DB. This file contains column headers as the first line of the file. So while loading the file into the DB through CSVREAD command, H2 is trying to parse the first line also and thus failing.

So how to skip loading the first line. Below the query I am using to load the file to the DB:

"CREATE TABLE TEST (CIRCLE VARCHAR_IGNORECASE(50), MSISDN VARCHAR_IGNORECASE(50), PORT_IN_DATE TIMESTAMP, OPERATOR VARCHAR_IGNORECASE(255), PRODUCT_TYPE VARCHAR_IGNORECASE(255), PORT_ID VARCHAR_IGNORECASE(255)) AS SELECT * FROM CSVREAD('src/test/resources/test.csv', "
1
  • 1
    The statement in your question is truncated; could you add the missing part? Please also add newlines if possible so it's easier to read. Commented Jan 6, 2012 at 20:50

1 Answer 1

17

The CSVREAD function supports both files with and without column header. If the file contains a column header, then don't supply the column list in the function, for example:

SELECT * FROM CSVREAD('test.csv');
SELECT * FROM CSVREAD('data/test.tsv', null, 'rowSeparator=' || CHAR(9));

and if the file doesn't contains the column header, then supply the column list in the function call, for example:

SELECT * FROM CSVREAD('test2.csv', 'ID|NAME', 'charset=UTF-8 fieldSeparator=|');
Sign up to request clarification or add additional context in comments.

2 Comments

For tsv file fieldSeparator should be char(9) instead of rowSeparator, hence SELECT * FROM CSVREAD('data/test.tsv', null, 'fieldSeparator=' || CHAR(9)); is correct for tsv.
What does the | mean in that fieldSeparator?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.