MySQL load NULL values from CSV data
I have a file that can contain from 3 to 4 columns of numerical values which are separated by comma. Empty fields are defined with the exception when they are at the end of the row:
1,2,3,4,5
1,2,3,,5
1,2,3
The following table was created in MySQL:
+-------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| one | int(1) | YES | | NULL | |
| two | int(1) | YES | | NULL | |
| three | int(1) | YES | | NULL | |
| four | int(1) | YES | | NULL | |
| five | int(1) | YES | | NULL | |
+-------+--------+------+-----+---------+-------+
I am trying to load the data using MySQL LOAD command:
LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo FIELDS
TERMINATED BY "," LINES TERMINATED BY "n";
The resulting table:
+------+------+-------+------+------+
| one | two | three | four | five |
+------+------+-------+------+------+
| 1 | 2 | 3 | 4 | 5 |
| 1 | 2 | 3 | 0 | 5 |
| 1 | 2 | 3 | NULL | NULL |
+------+------+-------+------+------+
The problem lies with the fact that when a field is empty in the raw data and is not defined, MySQL for some reason does not use the columns default value (which is NULL) and uses zero. NULL is used correctly when the field is missing alltogether.
Unfortunately, I have to be able to distinguish between NULL and 0 at this stage so any help would be appreciated.
Thanks
S.
edit
The output of SHOW WARNINGS:
+---------+------+--------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: '' for column 'four' at row 2 |
| Warning | 1261 | Row 3 doesn't contain data for all columns |
| Warning | 1261 | Row 3 doesn't contain data for all columns |
+---------+------+--------------------------------------------------------+
mysql csv load-data-infile
add a comment |
I have a file that can contain from 3 to 4 columns of numerical values which are separated by comma. Empty fields are defined with the exception when they are at the end of the row:
1,2,3,4,5
1,2,3,,5
1,2,3
The following table was created in MySQL:
+-------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| one | int(1) | YES | | NULL | |
| two | int(1) | YES | | NULL | |
| three | int(1) | YES | | NULL | |
| four | int(1) | YES | | NULL | |
| five | int(1) | YES | | NULL | |
+-------+--------+------+-----+---------+-------+
I am trying to load the data using MySQL LOAD command:
LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo FIELDS
TERMINATED BY "," LINES TERMINATED BY "n";
The resulting table:
+------+------+-------+------+------+
| one | two | three | four | five |
+------+------+-------+------+------+
| 1 | 2 | 3 | 4 | 5 |
| 1 | 2 | 3 | 0 | 5 |
| 1 | 2 | 3 | NULL | NULL |
+------+------+-------+------+------+
The problem lies with the fact that when a field is empty in the raw data and is not defined, MySQL for some reason does not use the columns default value (which is NULL) and uses zero. NULL is used correctly when the field is missing alltogether.
Unfortunately, I have to be able to distinguish between NULL and 0 at this stage so any help would be appreciated.
Thanks
S.
edit
The output of SHOW WARNINGS:
+---------+------+--------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: '' for column 'four' at row 2 |
| Warning | 1261 | Row 3 doesn't contain data for all columns |
| Warning | 1261 | Row 3 doesn't contain data for all columns |
+---------+------+--------------------------------------------------------+
mysql csv load-data-infile
With data schema changes like that I would use d6tstack which aligns all columns before runningLOAD DATA. See d6tstack SQL examples section on data schema changes.
– citynorman
Oct 14 '18 at 22:57
add a comment |
I have a file that can contain from 3 to 4 columns of numerical values which are separated by comma. Empty fields are defined with the exception when they are at the end of the row:
1,2,3,4,5
1,2,3,,5
1,2,3
The following table was created in MySQL:
+-------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| one | int(1) | YES | | NULL | |
| two | int(1) | YES | | NULL | |
| three | int(1) | YES | | NULL | |
| four | int(1) | YES | | NULL | |
| five | int(1) | YES | | NULL | |
+-------+--------+------+-----+---------+-------+
I am trying to load the data using MySQL LOAD command:
LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo FIELDS
TERMINATED BY "," LINES TERMINATED BY "n";
The resulting table:
+------+------+-------+------+------+
| one | two | three | four | five |
+------+------+-------+------+------+
| 1 | 2 | 3 | 4 | 5 |
| 1 | 2 | 3 | 0 | 5 |
| 1 | 2 | 3 | NULL | NULL |
+------+------+-------+------+------+
The problem lies with the fact that when a field is empty in the raw data and is not defined, MySQL for some reason does not use the columns default value (which is NULL) and uses zero. NULL is used correctly when the field is missing alltogether.
Unfortunately, I have to be able to distinguish between NULL and 0 at this stage so any help would be appreciated.
Thanks
S.
edit
The output of SHOW WARNINGS:
+---------+------+--------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: '' for column 'four' at row 2 |
| Warning | 1261 | Row 3 doesn't contain data for all columns |
| Warning | 1261 | Row 3 doesn't contain data for all columns |
+---------+------+--------------------------------------------------------+
mysql csv load-data-infile
I have a file that can contain from 3 to 4 columns of numerical values which are separated by comma. Empty fields are defined with the exception when they are at the end of the row:
1,2,3,4,5
1,2,3,,5
1,2,3
The following table was created in MySQL:
+-------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| one | int(1) | YES | | NULL | |
| two | int(1) | YES | | NULL | |
| three | int(1) | YES | | NULL | |
| four | int(1) | YES | | NULL | |
| five | int(1) | YES | | NULL | |
+-------+--------+------+-----+---------+-------+
I am trying to load the data using MySQL LOAD command:
LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo FIELDS
TERMINATED BY "," LINES TERMINATED BY "n";
The resulting table:
+------+------+-------+------+------+
| one | two | three | four | five |
+------+------+-------+------+------+
| 1 | 2 | 3 | 4 | 5 |
| 1 | 2 | 3 | 0 | 5 |
| 1 | 2 | 3 | NULL | NULL |
+------+------+-------+------+------+
The problem lies with the fact that when a field is empty in the raw data and is not defined, MySQL for some reason does not use the columns default value (which is NULL) and uses zero. NULL is used correctly when the field is missing alltogether.
Unfortunately, I have to be able to distinguish between NULL and 0 at this stage so any help would be appreciated.
Thanks
S.
edit
The output of SHOW WARNINGS:
+---------+------+--------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: '' for column 'four' at row 2 |
| Warning | 1261 | Row 3 doesn't contain data for all columns |
| Warning | 1261 | Row 3 doesn't contain data for all columns |
+---------+------+--------------------------------------------------------+
mysql csv load-data-infile
mysql csv load-data-infile
edited Aug 28 '16 at 11:47
Spiros
asked Apr 20 '10 at 13:11
SpirosSpiros
9252721
9252721
With data schema changes like that I would use d6tstack which aligns all columns before runningLOAD DATA. See d6tstack SQL examples section on data schema changes.
– citynorman
Oct 14 '18 at 22:57
add a comment |
With data schema changes like that I would use d6tstack which aligns all columns before runningLOAD DATA. See d6tstack SQL examples section on data schema changes.
– citynorman
Oct 14 '18 at 22:57
With data schema changes like that I would use d6tstack which aligns all columns before running
LOAD DATA. See d6tstack SQL examples section on data schema changes.– citynorman
Oct 14 '18 at 22:57
With data schema changes like that I would use d6tstack which aligns all columns before running
LOAD DATA. See d6tstack SQL examples section on data schema changes.– citynorman
Oct 14 '18 at 22:57
add a comment |
4 Answers
4
active
oldest
votes
This will do what you want. It reads the fourth field into a local variable, and then sets the actual field value to NULL, if the local variable ends up containing an empty string:
LOAD DATA infile '/tmp/testdata.txt'
INTO TABLE moo
fields terminated BY ","
lines terminated BY "n"
(one, two, three, @vfour, five)
SET four = nullif(@vfour,'')
;
If they're all possibly empty, then you'd read them all into variables and have multiple SET statements, like this:
LOAD DATA infile '/tmp/testdata.txt'
INTO TABLE moo
fields terminated BY ","
lines terminated BY "n"
(@vone, @vtwo, @vthree, @vfour, @vfive)
SET
one = nullif(@vone,''),
two = nullif(@vtwo,''),
three = nullif(@vthree,''),
four = nullif(@vfour,'')
;
does it have a performance impact?
– Blacksonic
Apr 18 '13 at 7:48
Theoretically, I suppose - but it's all in-memory, and only holding tiny amounts of data per row, so I would image it would be infinitesimal; but you should test it if you think it might be a problem.
– Duncan Lock
Apr 19 '13 at 3:00
2
I really like this answer. Users can see empty strings''when they download a csv (usingIFNULL(Col,'')inSELECT INTO OUTFILEquery) for excel but then uploads accept them as null vs having to deal withNin the csv. Thanks!
– chrisan
Sep 29 '13 at 15:47
7
for dates I used 'NULLIF(STR_TO_DATE(@date1, "%d/%m/%Y"), "0000-00-00")'
– Joaquín L. Robles
Feb 23 '14 at 23:20
Very much prefer this solution to the accepted answer, as this avoids the need to change source data.
– Yardboy
May 28 '14 at 18:21
|
show 5 more comments
MySQL manual says:
When reading data with LOAD DATA
INFILE, empty or missing columns are
updated with ''. If you want a NULL
value in a column, you should use N
in the data file. The literal word
“NULL” may also be used under some
circumstances.
So you need to replace the blanks with N like this:
1,2,3,4,5
1,2,3,N,5
1,2,3
3
Thanks for the tip - I am sceptical to edit the raw source data but if this is the only way around it I will try it out.
– Spiros
Apr 20 '10 at 13:55
7
I understand your scepticism, no one likes editing raw data, it just doesn't feel right. However, if you think about it for a minute, there has to be a way to distinguish between NULL and empty string. Should blank entries be translated to NULLs, you'd need a special sequence for empty string. It would nice to have a way how to tell MySQL how to treat blank entries though, something like LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo TREAT BLANKS AS NULL...
– Janci
Apr 20 '10 at 14:17
2
OK, but if you haveFields enclosed by: "is that"N"of"name",N,"stuff"
– Jonathon
Aug 25 '13 at 1:42
3
I can verify that at least for "phpMyAdmin 3.5.5" no style ofNis accepted as denotingNULL. Instead useNULL, as in this example:"name","age",NULL,"other","stuff"
– Jonathon
Aug 25 '13 at 2:05
1
We have MySQL 5.5.46-0+deb8u1. I tried both NULL and N, and only N worked for us.
– raphael75
Jun 30 '16 at 12:02
|
show 1 more comment
The behaviour is different depending upon the database configuration. In the strict mode this would throw an error else a warning.
Following query may be used for identifying the database configuration.
mysql> show variables like 'sql_mode';
Thanks! I was scratching my head trying to work out why importing a CSV with empty columns I'd successfully imported on the production server yesterday wasn't working on my brand-new local installation - this was the answer in my case!
– Emma Burrows
Jun 13 '16 at 15:54
add a comment |
Preprocess your input CSV to replace blank entries with N.
Attempt at a regex: s/,,/,n,/g and s/,$/,N/g
Good luck.
1
This regex partially works, it doesn't solve sequential blank entries, for example ,,,, will be ,n,,n, Should be usable if you run it twice
– ievgen
Jun 22 '16 at 19:12
Will summarize the answer and previous comment. Following worked for me, in the order: sed -i 's/,,/,N/g' $file, sed -i 's/,,/,/g' $file, sed -i 's/N,$/N/g' $file,
– Omar Khazamov
Dec 3 '16 at 23:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2675323%2fmysql-load-null-values-from-csv-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
This will do what you want. It reads the fourth field into a local variable, and then sets the actual field value to NULL, if the local variable ends up containing an empty string:
LOAD DATA infile '/tmp/testdata.txt'
INTO TABLE moo
fields terminated BY ","
lines terminated BY "n"
(one, two, three, @vfour, five)
SET four = nullif(@vfour,'')
;
If they're all possibly empty, then you'd read them all into variables and have multiple SET statements, like this:
LOAD DATA infile '/tmp/testdata.txt'
INTO TABLE moo
fields terminated BY ","
lines terminated BY "n"
(@vone, @vtwo, @vthree, @vfour, @vfive)
SET
one = nullif(@vone,''),
two = nullif(@vtwo,''),
three = nullif(@vthree,''),
four = nullif(@vfour,'')
;
does it have a performance impact?
– Blacksonic
Apr 18 '13 at 7:48
Theoretically, I suppose - but it's all in-memory, and only holding tiny amounts of data per row, so I would image it would be infinitesimal; but you should test it if you think it might be a problem.
– Duncan Lock
Apr 19 '13 at 3:00
2
I really like this answer. Users can see empty strings''when they download a csv (usingIFNULL(Col,'')inSELECT INTO OUTFILEquery) for excel but then uploads accept them as null vs having to deal withNin the csv. Thanks!
– chrisan
Sep 29 '13 at 15:47
7
for dates I used 'NULLIF(STR_TO_DATE(@date1, "%d/%m/%Y"), "0000-00-00")'
– Joaquín L. Robles
Feb 23 '14 at 23:20
Very much prefer this solution to the accepted answer, as this avoids the need to change source data.
– Yardboy
May 28 '14 at 18:21
|
show 5 more comments
This will do what you want. It reads the fourth field into a local variable, and then sets the actual field value to NULL, if the local variable ends up containing an empty string:
LOAD DATA infile '/tmp/testdata.txt'
INTO TABLE moo
fields terminated BY ","
lines terminated BY "n"
(one, two, three, @vfour, five)
SET four = nullif(@vfour,'')
;
If they're all possibly empty, then you'd read them all into variables and have multiple SET statements, like this:
LOAD DATA infile '/tmp/testdata.txt'
INTO TABLE moo
fields terminated BY ","
lines terminated BY "n"
(@vone, @vtwo, @vthree, @vfour, @vfive)
SET
one = nullif(@vone,''),
two = nullif(@vtwo,''),
three = nullif(@vthree,''),
four = nullif(@vfour,'')
;
does it have a performance impact?
– Blacksonic
Apr 18 '13 at 7:48
Theoretically, I suppose - but it's all in-memory, and only holding tiny amounts of data per row, so I would image it would be infinitesimal; but you should test it if you think it might be a problem.
– Duncan Lock
Apr 19 '13 at 3:00
2
I really like this answer. Users can see empty strings''when they download a csv (usingIFNULL(Col,'')inSELECT INTO OUTFILEquery) for excel but then uploads accept them as null vs having to deal withNin the csv. Thanks!
– chrisan
Sep 29 '13 at 15:47
7
for dates I used 'NULLIF(STR_TO_DATE(@date1, "%d/%m/%Y"), "0000-00-00")'
– Joaquín L. Robles
Feb 23 '14 at 23:20
Very much prefer this solution to the accepted answer, as this avoids the need to change source data.
– Yardboy
May 28 '14 at 18:21
|
show 5 more comments
This will do what you want. It reads the fourth field into a local variable, and then sets the actual field value to NULL, if the local variable ends up containing an empty string:
LOAD DATA infile '/tmp/testdata.txt'
INTO TABLE moo
fields terminated BY ","
lines terminated BY "n"
(one, two, three, @vfour, five)
SET four = nullif(@vfour,'')
;
If they're all possibly empty, then you'd read them all into variables and have multiple SET statements, like this:
LOAD DATA infile '/tmp/testdata.txt'
INTO TABLE moo
fields terminated BY ","
lines terminated BY "n"
(@vone, @vtwo, @vthree, @vfour, @vfive)
SET
one = nullif(@vone,''),
two = nullif(@vtwo,''),
three = nullif(@vthree,''),
four = nullif(@vfour,'')
;
This will do what you want. It reads the fourth field into a local variable, and then sets the actual field value to NULL, if the local variable ends up containing an empty string:
LOAD DATA infile '/tmp/testdata.txt'
INTO TABLE moo
fields terminated BY ","
lines terminated BY "n"
(one, two, three, @vfour, five)
SET four = nullif(@vfour,'')
;
If they're all possibly empty, then you'd read them all into variables and have multiple SET statements, like this:
LOAD DATA infile '/tmp/testdata.txt'
INTO TABLE moo
fields terminated BY ","
lines terminated BY "n"
(@vone, @vtwo, @vthree, @vfour, @vfive)
SET
one = nullif(@vone,''),
two = nullif(@vtwo,''),
three = nullif(@vthree,''),
four = nullif(@vfour,'')
;
edited Jul 29 '13 at 6:53
answered May 11 '11 at 17:59
Duncan LockDuncan Lock
7,54452640
7,54452640
does it have a performance impact?
– Blacksonic
Apr 18 '13 at 7:48
Theoretically, I suppose - but it's all in-memory, and only holding tiny amounts of data per row, so I would image it would be infinitesimal; but you should test it if you think it might be a problem.
– Duncan Lock
Apr 19 '13 at 3:00
2
I really like this answer. Users can see empty strings''when they download a csv (usingIFNULL(Col,'')inSELECT INTO OUTFILEquery) for excel but then uploads accept them as null vs having to deal withNin the csv. Thanks!
– chrisan
Sep 29 '13 at 15:47
7
for dates I used 'NULLIF(STR_TO_DATE(@date1, "%d/%m/%Y"), "0000-00-00")'
– Joaquín L. Robles
Feb 23 '14 at 23:20
Very much prefer this solution to the accepted answer, as this avoids the need to change source data.
– Yardboy
May 28 '14 at 18:21
|
show 5 more comments
does it have a performance impact?
– Blacksonic
Apr 18 '13 at 7:48
Theoretically, I suppose - but it's all in-memory, and only holding tiny amounts of data per row, so I would image it would be infinitesimal; but you should test it if you think it might be a problem.
– Duncan Lock
Apr 19 '13 at 3:00
2
I really like this answer. Users can see empty strings''when they download a csv (usingIFNULL(Col,'')inSELECT INTO OUTFILEquery) for excel but then uploads accept them as null vs having to deal withNin the csv. Thanks!
– chrisan
Sep 29 '13 at 15:47
7
for dates I used 'NULLIF(STR_TO_DATE(@date1, "%d/%m/%Y"), "0000-00-00")'
– Joaquín L. Robles
Feb 23 '14 at 23:20
Very much prefer this solution to the accepted answer, as this avoids the need to change source data.
– Yardboy
May 28 '14 at 18:21
does it have a performance impact?
– Blacksonic
Apr 18 '13 at 7:48
does it have a performance impact?
– Blacksonic
Apr 18 '13 at 7:48
Theoretically, I suppose - but it's all in-memory, and only holding tiny amounts of data per row, so I would image it would be infinitesimal; but you should test it if you think it might be a problem.
– Duncan Lock
Apr 19 '13 at 3:00
Theoretically, I suppose - but it's all in-memory, and only holding tiny amounts of data per row, so I would image it would be infinitesimal; but you should test it if you think it might be a problem.
– Duncan Lock
Apr 19 '13 at 3:00
2
2
I really like this answer. Users can see empty strings
'' when they download a csv (using IFNULL(Col,'') in SELECT INTO OUTFILE query) for excel but then uploads accept them as null vs having to deal with N in the csv. Thanks!– chrisan
Sep 29 '13 at 15:47
I really like this answer. Users can see empty strings
'' when they download a csv (using IFNULL(Col,'') in SELECT INTO OUTFILE query) for excel but then uploads accept them as null vs having to deal with N in the csv. Thanks!– chrisan
Sep 29 '13 at 15:47
7
7
for dates I used 'NULLIF(STR_TO_DATE(@date1, "%d/%m/%Y"), "0000-00-00")'
– Joaquín L. Robles
Feb 23 '14 at 23:20
for dates I used 'NULLIF(STR_TO_DATE(@date1, "%d/%m/%Y"), "0000-00-00")'
– Joaquín L. Robles
Feb 23 '14 at 23:20
Very much prefer this solution to the accepted answer, as this avoids the need to change source data.
– Yardboy
May 28 '14 at 18:21
Very much prefer this solution to the accepted answer, as this avoids the need to change source data.
– Yardboy
May 28 '14 at 18:21
|
show 5 more comments
MySQL manual says:
When reading data with LOAD DATA
INFILE, empty or missing columns are
updated with ''. If you want a NULL
value in a column, you should use N
in the data file. The literal word
“NULL” may also be used under some
circumstances.
So you need to replace the blanks with N like this:
1,2,3,4,5
1,2,3,N,5
1,2,3
3
Thanks for the tip - I am sceptical to edit the raw source data but if this is the only way around it I will try it out.
– Spiros
Apr 20 '10 at 13:55
7
I understand your scepticism, no one likes editing raw data, it just doesn't feel right. However, if you think about it for a minute, there has to be a way to distinguish between NULL and empty string. Should blank entries be translated to NULLs, you'd need a special sequence for empty string. It would nice to have a way how to tell MySQL how to treat blank entries though, something like LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo TREAT BLANKS AS NULL...
– Janci
Apr 20 '10 at 14:17
2
OK, but if you haveFields enclosed by: "is that"N"of"name",N,"stuff"
– Jonathon
Aug 25 '13 at 1:42
3
I can verify that at least for "phpMyAdmin 3.5.5" no style ofNis accepted as denotingNULL. Instead useNULL, as in this example:"name","age",NULL,"other","stuff"
– Jonathon
Aug 25 '13 at 2:05
1
We have MySQL 5.5.46-0+deb8u1. I tried both NULL and N, and only N worked for us.
– raphael75
Jun 30 '16 at 12:02
|
show 1 more comment
MySQL manual says:
When reading data with LOAD DATA
INFILE, empty or missing columns are
updated with ''. If you want a NULL
value in a column, you should use N
in the data file. The literal word
“NULL” may also be used under some
circumstances.
So you need to replace the blanks with N like this:
1,2,3,4,5
1,2,3,N,5
1,2,3
3
Thanks for the tip - I am sceptical to edit the raw source data but if this is the only way around it I will try it out.
– Spiros
Apr 20 '10 at 13:55
7
I understand your scepticism, no one likes editing raw data, it just doesn't feel right. However, if you think about it for a minute, there has to be a way to distinguish between NULL and empty string. Should blank entries be translated to NULLs, you'd need a special sequence for empty string. It would nice to have a way how to tell MySQL how to treat blank entries though, something like LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo TREAT BLANKS AS NULL...
– Janci
Apr 20 '10 at 14:17
2
OK, but if you haveFields enclosed by: "is that"N"of"name",N,"stuff"
– Jonathon
Aug 25 '13 at 1:42
3
I can verify that at least for "phpMyAdmin 3.5.5" no style ofNis accepted as denotingNULL. Instead useNULL, as in this example:"name","age",NULL,"other","stuff"
– Jonathon
Aug 25 '13 at 2:05
1
We have MySQL 5.5.46-0+deb8u1. I tried both NULL and N, and only N worked for us.
– raphael75
Jun 30 '16 at 12:02
|
show 1 more comment
MySQL manual says:
When reading data with LOAD DATA
INFILE, empty or missing columns are
updated with ''. If you want a NULL
value in a column, you should use N
in the data file. The literal word
“NULL” may also be used under some
circumstances.
So you need to replace the blanks with N like this:
1,2,3,4,5
1,2,3,N,5
1,2,3
MySQL manual says:
When reading data with LOAD DATA
INFILE, empty or missing columns are
updated with ''. If you want a NULL
value in a column, you should use N
in the data file. The literal word
“NULL” may also be used under some
circumstances.
So you need to replace the blanks with N like this:
1,2,3,4,5
1,2,3,N,5
1,2,3
edited Apr 20 '10 at 13:44
answered Apr 20 '10 at 13:36
JanciJanci
2,24511520
2,24511520
3
Thanks for the tip - I am sceptical to edit the raw source data but if this is the only way around it I will try it out.
– Spiros
Apr 20 '10 at 13:55
7
I understand your scepticism, no one likes editing raw data, it just doesn't feel right. However, if you think about it for a minute, there has to be a way to distinguish between NULL and empty string. Should blank entries be translated to NULLs, you'd need a special sequence for empty string. It would nice to have a way how to tell MySQL how to treat blank entries though, something like LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo TREAT BLANKS AS NULL...
– Janci
Apr 20 '10 at 14:17
2
OK, but if you haveFields enclosed by: "is that"N"of"name",N,"stuff"
– Jonathon
Aug 25 '13 at 1:42
3
I can verify that at least for "phpMyAdmin 3.5.5" no style ofNis accepted as denotingNULL. Instead useNULL, as in this example:"name","age",NULL,"other","stuff"
– Jonathon
Aug 25 '13 at 2:05
1
We have MySQL 5.5.46-0+deb8u1. I tried both NULL and N, and only N worked for us.
– raphael75
Jun 30 '16 at 12:02
|
show 1 more comment
3
Thanks for the tip - I am sceptical to edit the raw source data but if this is the only way around it I will try it out.
– Spiros
Apr 20 '10 at 13:55
7
I understand your scepticism, no one likes editing raw data, it just doesn't feel right. However, if you think about it for a minute, there has to be a way to distinguish between NULL and empty string. Should blank entries be translated to NULLs, you'd need a special sequence for empty string. It would nice to have a way how to tell MySQL how to treat blank entries though, something like LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo TREAT BLANKS AS NULL...
– Janci
Apr 20 '10 at 14:17
2
OK, but if you haveFields enclosed by: "is that"N"of"name",N,"stuff"
– Jonathon
Aug 25 '13 at 1:42
3
I can verify that at least for "phpMyAdmin 3.5.5" no style ofNis accepted as denotingNULL. Instead useNULL, as in this example:"name","age",NULL,"other","stuff"
– Jonathon
Aug 25 '13 at 2:05
1
We have MySQL 5.5.46-0+deb8u1. I tried both NULL and N, and only N worked for us.
– raphael75
Jun 30 '16 at 12:02
3
3
Thanks for the tip - I am sceptical to edit the raw source data but if this is the only way around it I will try it out.
– Spiros
Apr 20 '10 at 13:55
Thanks for the tip - I am sceptical to edit the raw source data but if this is the only way around it I will try it out.
– Spiros
Apr 20 '10 at 13:55
7
7
I understand your scepticism, no one likes editing raw data, it just doesn't feel right. However, if you think about it for a minute, there has to be a way to distinguish between NULL and empty string. Should blank entries be translated to NULLs, you'd need a special sequence for empty string. It would nice to have a way how to tell MySQL how to treat blank entries though, something like LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo TREAT BLANKS AS NULL...
– Janci
Apr 20 '10 at 14:17
I understand your scepticism, no one likes editing raw data, it just doesn't feel right. However, if you think about it for a minute, there has to be a way to distinguish between NULL and empty string. Should blank entries be translated to NULLs, you'd need a special sequence for empty string. It would nice to have a way how to tell MySQL how to treat blank entries though, something like LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo TREAT BLANKS AS NULL...
– Janci
Apr 20 '10 at 14:17
2
2
OK, but if you have
Fields enclosed by: " is that "N" of "name",N,"stuff"– Jonathon
Aug 25 '13 at 1:42
OK, but if you have
Fields enclosed by: " is that "N" of "name",N,"stuff"– Jonathon
Aug 25 '13 at 1:42
3
3
I can verify that at least for "phpMyAdmin 3.5.5" no style of
N is accepted as denoting NULL. Instead use NULL, as in this example: "name","age",NULL,"other","stuff"– Jonathon
Aug 25 '13 at 2:05
I can verify that at least for "phpMyAdmin 3.5.5" no style of
N is accepted as denoting NULL. Instead use NULL, as in this example: "name","age",NULL,"other","stuff"– Jonathon
Aug 25 '13 at 2:05
1
1
We have MySQL 5.5.46-0+deb8u1. I tried both NULL and N, and only N worked for us.
– raphael75
Jun 30 '16 at 12:02
We have MySQL 5.5.46-0+deb8u1. I tried both NULL and N, and only N worked for us.
– raphael75
Jun 30 '16 at 12:02
|
show 1 more comment
The behaviour is different depending upon the database configuration. In the strict mode this would throw an error else a warning.
Following query may be used for identifying the database configuration.
mysql> show variables like 'sql_mode';
Thanks! I was scratching my head trying to work out why importing a CSV with empty columns I'd successfully imported on the production server yesterday wasn't working on my brand-new local installation - this was the answer in my case!
– Emma Burrows
Jun 13 '16 at 15:54
add a comment |
The behaviour is different depending upon the database configuration. In the strict mode this would throw an error else a warning.
Following query may be used for identifying the database configuration.
mysql> show variables like 'sql_mode';
Thanks! I was scratching my head trying to work out why importing a CSV with empty columns I'd successfully imported on the production server yesterday wasn't working on my brand-new local installation - this was the answer in my case!
– Emma Burrows
Jun 13 '16 at 15:54
add a comment |
The behaviour is different depending upon the database configuration. In the strict mode this would throw an error else a warning.
Following query may be used for identifying the database configuration.
mysql> show variables like 'sql_mode';
The behaviour is different depending upon the database configuration. In the strict mode this would throw an error else a warning.
Following query may be used for identifying the database configuration.
mysql> show variables like 'sql_mode';
answered Jan 22 '15 at 17:10
DobiDobi
6115
6115
Thanks! I was scratching my head trying to work out why importing a CSV with empty columns I'd successfully imported on the production server yesterday wasn't working on my brand-new local installation - this was the answer in my case!
– Emma Burrows
Jun 13 '16 at 15:54
add a comment |
Thanks! I was scratching my head trying to work out why importing a CSV with empty columns I'd successfully imported on the production server yesterday wasn't working on my brand-new local installation - this was the answer in my case!
– Emma Burrows
Jun 13 '16 at 15:54
Thanks! I was scratching my head trying to work out why importing a CSV with empty columns I'd successfully imported on the production server yesterday wasn't working on my brand-new local installation - this was the answer in my case!
– Emma Burrows
Jun 13 '16 at 15:54
Thanks! I was scratching my head trying to work out why importing a CSV with empty columns I'd successfully imported on the production server yesterday wasn't working on my brand-new local installation - this was the answer in my case!
– Emma Burrows
Jun 13 '16 at 15:54
add a comment |
Preprocess your input CSV to replace blank entries with N.
Attempt at a regex: s/,,/,n,/g and s/,$/,N/g
Good luck.
1
This regex partially works, it doesn't solve sequential blank entries, for example ,,,, will be ,n,,n, Should be usable if you run it twice
– ievgen
Jun 22 '16 at 19:12
Will summarize the answer and previous comment. Following worked for me, in the order: sed -i 's/,,/,N/g' $file, sed -i 's/,,/,/g' $file, sed -i 's/N,$/N/g' $file,
– Omar Khazamov
Dec 3 '16 at 23:43
add a comment |
Preprocess your input CSV to replace blank entries with N.
Attempt at a regex: s/,,/,n,/g and s/,$/,N/g
Good luck.
1
This regex partially works, it doesn't solve sequential blank entries, for example ,,,, will be ,n,,n, Should be usable if you run it twice
– ievgen
Jun 22 '16 at 19:12
Will summarize the answer and previous comment. Following worked for me, in the order: sed -i 's/,,/,N/g' $file, sed -i 's/,,/,/g' $file, sed -i 's/N,$/N/g' $file,
– Omar Khazamov
Dec 3 '16 at 23:43
add a comment |
Preprocess your input CSV to replace blank entries with N.
Attempt at a regex: s/,,/,n,/g and s/,$/,N/g
Good luck.
Preprocess your input CSV to replace blank entries with N.
Attempt at a regex: s/,,/,n,/g and s/,$/,N/g
Good luck.
answered Apr 20 '10 at 13:29
Sam GoldmanSam Goldman
1,1661013
1,1661013
1
This regex partially works, it doesn't solve sequential blank entries, for example ,,,, will be ,n,,n, Should be usable if you run it twice
– ievgen
Jun 22 '16 at 19:12
Will summarize the answer and previous comment. Following worked for me, in the order: sed -i 's/,,/,N/g' $file, sed -i 's/,,/,/g' $file, sed -i 's/N,$/N/g' $file,
– Omar Khazamov
Dec 3 '16 at 23:43
add a comment |
1
This regex partially works, it doesn't solve sequential blank entries, for example ,,,, will be ,n,,n, Should be usable if you run it twice
– ievgen
Jun 22 '16 at 19:12
Will summarize the answer and previous comment. Following worked for me, in the order: sed -i 's/,,/,N/g' $file, sed -i 's/,,/,/g' $file, sed -i 's/N,$/N/g' $file,
– Omar Khazamov
Dec 3 '16 at 23:43
1
1
This regex partially works, it doesn't solve sequential blank entries, for example ,,,, will be ,n,,n, Should be usable if you run it twice
– ievgen
Jun 22 '16 at 19:12
This regex partially works, it doesn't solve sequential blank entries, for example ,,,, will be ,n,,n, Should be usable if you run it twice
– ievgen
Jun 22 '16 at 19:12
Will summarize the answer and previous comment. Following worked for me, in the order: sed -i 's/,,/,N/g' $file, sed -i 's/,,/,/g' $file, sed -i 's/N,$/N/g' $file,
– Omar Khazamov
Dec 3 '16 at 23:43
Will summarize the answer and previous comment. Following worked for me, in the order: sed -i 's/,,/,N/g' $file, sed -i 's/,,/,/g' $file, sed -i 's/N,$/N/g' $file,
– Omar Khazamov
Dec 3 '16 at 23:43
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2675323%2fmysql-load-null-values-from-csv-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
With data schema changes like that I would use d6tstack which aligns all columns before running
LOAD DATA. See d6tstack SQL examples section on data schema changes.– citynorman
Oct 14 '18 at 22:57