Weird characters showing up when importing from csv files in sql - sql

I'm trying to import data from a csv file into SQL but I keep getting the error
\copy owner (owner_id, owner_name, owner_surname) FROM 'C:\Users\Documents\owners.csv' DELIMITER ',';
ERROR: invalid input syntax for type integer: "0"
CONTEXT: COPY owner, line 1, column owner_id: "0"
Here's what owners.csv looks like
I understand that the error is to do with the encoding and that I should change the encoding to UTF 8 BOM, which I have done but the error still persist

0 in WIN1252 is hexadecimal 0xEFBBBF30, which would be a BOM and a 0.
Remove that BOM from the file, and you will get better results.

Related

Importing csv file with non english text return error on select data

I have succesfully import CSV file with pgAdmin and getting error when select data:
'utf-8' codec can't decode byte 0xd2 in position 0: unexpected end of data
Also, I try to select data from terminal shell, getting same error.
In pgAdmin, on importing CSV file, file encoding set to UTF-8.
Imported file was saved in UTF-8, when I open them on notepad text is readable.
Database encoding set to UTF-8 from pg Admin.
At the terminal, SET SERVER_ENCODING UTF-8; SHOW SERVER_ENCODING returns me UTF-8;
This is my example of data, that I am trying to select:
Header:
client_id;date;status_name;arrive_date
Data:
-621;31.12.2020;Қайта ұшып кетті;07.01.2021
I think psql cant read text in Kazakh lanuage like this "Қайта ұшып кетті".
UPD: I am trying to select withot text in Kazakh language, works fine, but with Kazakh text, return error with encoding.
Clien_encoding - UTF8
Server_encoding - UTF8
I have delet records, change encoding, same error.
How to solve this trouble ?
Problem is solving by changing column type from char to varchar. Now text select correctly

Line contains invalid enclosed character data or delimiter at position

I was trying to load the data from the csv file into the Oracle sql developer, when inserting the data I encountered the error which says:
Line contains invalid enclosed character data or delimiter at position
I am not sure how to tackle this problem!
For Example:
INSERT INTO PROJECT_LIST (Project_Number, Name, Manager, Projects_M,
Project_Type, In_progress, at_deck, Start_Date, release_date, For_work, nbr,
List, Expenses) VALUES ('5770','"Program Cardinal
(Agile)','','','','','',to_date('', 'YYYY-MM-DD'),'','','','','');
The Error shown were:
--Insert failed for row 4
--Line contains invalid enclosed character data or delimiter at position 79.
--Row 4
I've had success when I've converted the csv file to excel by "save as", then changing the format to .xlsx. I then load in SQL developer the .xlsx version. I think the conversion forces some of the bad formatting out. It worked at least on my last 2 files.
I fixed it by using the concatenate function in my CSV file first and then uploaded it on sql, which worked.
My guess is that it doesn't like to_date('', 'YYYY-MM-DD'). It's missing a date to format. Is that an actual input of your data?
But it could also possibly be the double quote in "Program Cardinal (Agile). Though I don't see why that would get picked up as an invalid character.

How to import from a mixed-encoding file to a PostgreSQL table

I have a 30 GB text file. the encoding of the file is UTF8 but it also contains some Windows-1252 characters. So, when I try to import, it gives the following error:
ERROR: invalid byte sequence for encoding "UTF8": 0x9b
How can I fix this?
the file already has UTF8 format, when i run the 'file' command for this file it says the encoding is UTF8. but it also contains some not UTF8 byte sequences. for example when I run the \copy command after a while it gives the above mentioned error for this row:
0B012234 Basic study of <img src="/fulltext-image.asp?format=htmlnonpaginated&src=323K744431152658_html\233_2 basic study of img src fulltext image asp format htmlnonpaginated src 323k744431152658_html 233_2 1975 Semigroup Forum semigroup forum 04861B53 19555
The issue is caused by the backslash (\).
Use CSV format which does not treat backslash as a special character, e.g. -
\copy t from myfile.txt with csv quote E'\x1' delimiter E'\x2'

COPY with exclude clause "invalid byte sequence for encoding "UTF8": 0xdf 0x4f"

I use COPY to import a file in my database
COPY mytable(c1, c2) FROM '/tmp/myfile.csv' WITH DELIMITER ';' CSV HEADER
And as usual, there are some "bad" caracters in the file wich generates encoding SQL error:
ERROR: invalid byte sequence for encoding "UTF8": 0xdf 0x4f
So I open the file and delete the line but, is there a way to COPY a file excluding by default this kind of line ?
Thanks for help

Loading huge csv file using COPY

I am loading CSV file using COPY.
COPY cts FROM 'C:\...\cts.csv' using DELIMITERS',';
However, error comes out
ERROR: invalid input syntax for type double precision: ""
CONTEXT: COPY testdata, line 7, column latitude: ""
How to fix it please?
Looks like your CSV isn't quite formatted correctly. "" isn't a number, and numbers don't need to be be quoted in CSV.
I find it's usually easier in PostgreSQL to create a staging import table with all text columns, and import CSVs to there first. Then do a cleanup query to put the CSV data into the real table.