cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Load CSV - Update a field if its not null in CSV

Kailash
Graph Buddy

I am trying to load data using the load csv. syntax might be wrong here.
<USING PERIODIC COMMIT 100 LOAD CSV WITH HEADERS FROM "file:///myfile.csv"
as eachline FIELDTERMINATOR ,
WITH eachline
"MATCH(r:Record) where r.Id contains eachline.Id
set r.Name=eachline.Id , r.EmailId=eachline.email , r.lastName=eachline.id />
Sample query above.

Here i want to SET/update the e.EmailId only if the value is present.
it may happen that email is updated and it is not present in the record next time when csv is generated. in that case i want to ignore the record and not set with empty field and update other fields.
could the ignoreme be used here?

5 REPLIES 5

You don't have to do all the sets in one go. You can set the values that must absolutely be set, then add the following afterwards:

WITH r, eachline
WHERE eachline.email IS NOT NULL
SET r.EmailId = eachline.email

That way that set will only execute for the lines where eachline.email is present.

Otherwise, if you don't want to go this route, you can use coalesce() instead, which will evaluate to the first non-null value encountered:

...
set r.Name=eachline.Id , r.EmailId= coalesce(eachline.email, r.EmailId) , r.lastName=eachline.id />

This way if eachline.email is null, then it will instead set the field to the fields current value.

Thanks for your email..!!
i can not go with the first one... but will try then next one..
here will it work if the eachline.email is updated with any new value instead of null/empty.
will it consider and update the new one? I will try as well. Thanks

also seems like coalesce will work to check if its null and not empty..
Example:
First load sets r.EmailId=abc.com from csv eachline.email
Next load of csv say eachline.email = " " , ignore update to this field(r.EmailId) as its empty. so set - r.EmailId= abc.com instead of " " and only update other fields on node.
Next load if csv file has eachline.email="xyz.com" then set existing r.EmailId to eachline.email(xyz.com)

You can use CASE instead for empty checks.

...
set r.Name=eachline.Id , r.EmailId= CASE WHEN eachline.email = ' ' THEN r.EmailId ELSE eachline.email END , r.lastName=eachline.id

@andrew.bowman - it worked fine for now. thanks for your help.
if anything.. will connect here.