I'm using DuckDB to read a CSV file that contains empty strings in some fields. However, DuckDB converts these empty strings to NULL.
CSV Sample Data:
"path","comment"
"","[{""comment"":""grand total"",""updatedByUser"":"",""dateTime"":""2025-03-27 09:08:38"",""threadStatus"":null,""threadAssignee"":null}]"
"East","[{""comment"":""Sub total"",""updatedByUser"":"",""dateTime"":""2025-03-27 09:09:03"",""threadStatus"":null,""threadAssignee"":null}]"
Query I'm Using:
SELECT * FROM read_csv('${path}', delim=',', header=True, columns={"path":"VARCHAR","comment":"VARCHAR"});
I want empty strings ("") in the path
column to remain empty strings, but they are instead being converted to NULL
s.
How can I have these values remain empty strings?
I'm using DuckDB to read a CSV file that contains empty strings in some fields. However, DuckDB converts these empty strings to NULL.
CSV Sample Data:
"path","comment"
"","[{""comment"":""grand total"",""updatedByUser"":"",""dateTime"":""2025-03-27 09:08:38"",""threadStatus"":null,""threadAssignee"":null}]"
"East","[{""comment"":""Sub total"",""updatedByUser"":"",""dateTime"":""2025-03-27 09:09:03"",""threadStatus"":null,""threadAssignee"":null}]"
Query I'm Using:
SELECT * FROM read_csv('${path}', delim=',', header=True, columns={"path":"VARCHAR","comment":"VARCHAR"});
I want empty strings ("") in the path
column to remain empty strings, but they are instead being converted to NULL
s.
How can I have these values remain empty strings?
Share Improve this question edited Apr 2 at 9:33 AD7six 66.5k14 gold badges110 silver badges156 bronze badges asked Apr 2 at 5:22 Ms.anonymousMs.anonymous 132 bronze badges New contributor Ms.anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 8 | Show 3 more comments2 Answers
Reset to default 1By default, DuckDB will treat empty strings as NULL
s upon import. This behavior can be modified using force_not_null:
Do not match values in the specified columns against the NULL string. In the default case where the NULL string is empty, this means that empty values are read as zero-length strings instead of NULLs.
You could use COALESCE to change the NULL values.
select
coalesce(path, '') as path
,comment
from read_csv('${path}')
updatedByUser
has a blank string. What's different about that field? – Tim Roberts Commented Apr 2 at 5:29