How to handle null from a text file in Mysql if the data type is Char(1)? - mysql

This is my first time trying Mysql. I created a table using the following command.
CREATE TABLE IF NOT EXISTS `COMPANY`.`EMPLOYEE` (
`Fname` VARCHAR(15) NOT NULL,
`Minit` CHAR NULL,
`Lname` VARCHAR(15) NOT NULL,
`Ssn` CHAR(9) NOT NULL,
`Bdate` DATE NULL,
`Address` VARCHAR(30) NULL,
`Sex` CHAR NULL,
`Salary` DECIMAL(10,2),
`Super_ssn` CHAR(9) NULL,
`Dno` INT NOT NULL,
PRIMARY KEY (`Ssn`));
And I was trying to fill the table using the command below:
LOAD DATA
INFILE 'C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\EMPLOYEE.txt'
INTO TABLE employee
FIELDS TERMINATED BY ', '
OPTIONALLY ENCLOSED BY ''''
LINES TERMINATED BY '\n'
(Fname,Minit,Lname,Ssn,#var2,Address,Sex,Salary,Super_ssn,Dno)
SET Bdate=str_to_date(#var2,'%d-%b-%Y');
And this is my text file
'James', 'E', 'Borg', '888665555', '10-NOV-1927', '450 Stone,Houston,TX', 'M', 55000, null, 1
'Franklin',null, 'Wong', '333445555', '08-DEC-1945', '638 Voss,Houston,TX', 'M', 40000, '888665555', 5
I am getting an error saying:
Error Code: 1406. Data too long for column 'Minit' at row 2
How do I solve this?

The problem is that you used optionally in the enclosed by setting. If the enclosing character is required, it will treat null with no quotes around it as a NULL value. Otherwise, it interprets it as the literal string "null", and requires you to use \N to specify a null value.
You could use an intermediate variable to translate it yourself.
LOAD DATA
INFILE 'C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\EMPLOYEE.txt'
INTO TABLE employee
FIELDS TERMINATED BY ', '
OPTIONALLY ENCLOSED BY ''''
LINES TERMINATED BY '\n'
(Fname,#Minit,Lname,Ssn,#var2,Address,Sex,Salary,Super_ssn,Dno)
SET Bdate=str_to_date(#var2,'%d-%b-%Y'), Minit = NULLIF(#Minit, 'null')

Related

How do I write a query in MYSQL to avoid qoutes from a CSV

I have a big .csv with over a million lines that I am trying to import into MYSQL
an example of a line is as follows: 50778,2020-04-26,f,"$2,655.00","$2,655.00",5,1125
This represents 7 fields: id, date, availability, price, adjustedPrice, minNights, maxNights.
I am having problems reading even one of them into a table with the correct columns and parameters for each
This is the query used to import:
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/calendar.csv'
INTO TABLE calendar
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
Right now I am getting data too long for column price at row 1
The table is:
CREATE TABLE calendar (
listing_id varchar(10) NOT NULL,
date date NOT NULL,
available varchar(1) NOT NULL,
price varchar(25) NOT NULL,
adjusted_price varchar(15) NOT NULL,
minimum_nights int(11) NOT NULL,
maximum_nights int(11) NOT NULL,
PRIMARY KEY (listing_i`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
See that price and adjusted price are varchars because they contain "$"

What to do about 'data truncated for column' warnings on csv import?

I am trying to import data from a text file into table in mysql-5.5. Here is the table create code:
CREATE TABLE `price` (
`symbol` VARCHAR(20) NOT NULL,
`date` DATE NOT NULL,
`open` DOUBLE NULL DEFAULT NULL,
`high` DOUBLE NULL DEFAULT NULL,
`low` DOUBLE NULL DEFAULT NULL,
`close` DOUBLE NULL DEFAULT NULL,
`volume` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
PRIMARY KEY (`date`, `symbol`)
)
ENGINE=InnoDB
;
Here is the import code.
INFILE 'c:/temp/DTB3.csv'
INTO TABLE price
FIELDS TERMINATED BY ','
(symbol, date, #dummy, #dummy, close);
Here are some rows of data.
10,2019-01-30,1.00,1.00,50459.4301
10,2019-01-31,1.00,1.00,50477.9307
10,2019-02-01,1.00,1.00,50496.4382
Here is a sample warning:
Warning 1,265 Data truncated for column 'close' at row 12
The data in the resulting table looks correct.
I have tried both double and float for the price fields.
Why am I getting these warnings and what can I do about them?
My best guess is that you have declared 7 columns in your table but giving data for only 5.
Try adding commas for the missing (null) fields in your import data so the import knows explicitly there's no value to load.
Like this:
10,2019-01-30,1.00,1.00,50459.4301,,
10,2019-01-30,1.00,1.00,50459.4301,,
10,2019-01-30,1.00,1.00,50459.4301,,
In case anyone else stumbles across this problem amending the query to add:
LINES TERMINATED BY '\r\n'
stopped the warnings. Apparently MySQL was trying to append the carriage return to the close field on import.

MYSQL query data is not displaying properly

hi guys im new to mysql, i have trouble with displaying my table data, not sure what i am doing wrong..
this is my query for creating table:
CREATE TABLE sales20102017(
date DATE NOT NULL,
address VARCHAR(60) NOT NULL,
postal_code VARCHAR(10) NULL,
county VARCHAR (15) NOT NULL,
price BIGINT UNSIGNED NOT NULL,
not_full_market_price ENUM('Y', 'N') NOT NULL,
vat_exclusive ENUM('Y', 'N') NOT NULL,
property_description VARCHAR (30) NOT NULL,
size_description VARCHAR(40) NULL
);
and my load data:
LOAD DATA INFILE "C:/xampp/mysql/bin/sales20102017.csv"
INTO TABLE sales20102017
FIELDS TERMINATED BY '\n'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
and my excel data:
excel data
this is the result i get when i display it on mysql
mysql result
the table is not in order and everything is just messed up
Check for fields containing commas in csv file import on Mysql.
LOAD DATA INFILE "C:/xampp/mysql/bin/sales20102017.csv"
INTO TABLE sales20102017
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
See more on MySQL Reference Manual.

how to import data into spcial character column name using LOAD DATA INFILE

under following sample im trying to import CSV data into MySQL table,
LOAD DATA INFILE 'file1.csv'
INTO TABLE loc_chg_log
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(#col1,#col2,#col3,#col4,#col5) set failename=#col1,path=#col2,==blank=#col3,!=blank=#col4,++code=#col5;
MySQL table query is like this,
CREATE TABLE `log`.`loc_chg_log` (
`id` INT NOT NULL AUTO_INCREMENT,
`filename` VARCHAR(225) NULL,
`path` VARCHAR(225) NULL,
`==blank` VARCHAR(45) NULL,
`!=blank` VARCHAR(125) NULL,
`++code` VARCHAR(45) NULL
PRIMARY KEY (`id`));
none of these methods work's since the column name includes special character's, im getting error "unknown column'==blank' in field list"
1. '==blank'=#col5
2. ("`!=blank`=#col7")
3. [==blank=#col5]
by '==blank'=#col5 putting backticks in the column name, I was able to write on data in MySQL database. thank you

Data truncated when trying to import CSV file

Hi here is my load statement:
load data local infile 'C:/GeoIPCountryWhois.csv' into table geolocation fields terminated by ','
enclosed by '"'
lines terminated by '\n';
30 row(s) affected, 1 warning(s):
1265 Data truncated for column 'cn' at row 30
Records: 30 Deleted: 0 Skipped: 0 Warnings: 1
CREATE TABLE `geolocation` (
`start_ip` char(15) NOT NULL,
`end_ip` char(15) NOT NULL,
`start` int(10) unsigned NOT NULL,
`end` int(10) unsigned NOT NULL,
`cc` char(2) NOT NULL,
`cn` varchar(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
I found that the problem is most likely the line below because of the comma in Korea it things that a new field starts.
1.11.0.0,1.11.255.255,17498112,17563647,KR,"Korea, Republic of"
I can remove all the commas but can I change the load statement somehow?
My data in mysql is ok, right now.
what I change:
in file GeoIPCountryWhois - i used in excel CTRL+H to remove "," from names like "Korea, Republic of" --> "Republic of Korea". and also i removed all "
i have to change in mysql
start bigint(10) unsigned NOT NULL,
end bigint(10) unsigned NOT NULL,
because was problem from "128.0.0.0" in tables "start" and "end" (this same max int size to end of this tables - duplicates)
importing csv from phpmyadmin :
(if you need translate, tell me..)
You should use OPTIONALLY ENCLOSED BY instead of ENCLOSED BY.
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Adding OPTIONALLY to the statement allows your CSV to be in its current form. Without the OPTIONALLY keyword, it is expected that every value will have quotations around it.