Ljubica Lazarevic
1 min readApr 24, 2019

--

Using default values for nulls

I recently was looking to load some data that had null values. However, I still wanted to set a property on my nodes and relationships with a default value, i.e. still create the value.

You may know the trick of using:

FOREACH (ignoreMe IN CASE WHEN line.Value IS NOT NULL THEN [1] ELSE [] END | <Enter here the values you want to set when not null>)

which manages the null data values. However, this will not create properties when a value is null (and indeed this may well be the behaviour you want!).

In the example I was working on, I did want to set a value for when the input value was null, but to a default one. I wanted to have two dates, a FromDate and a ToDate. If a ToDate didn’t exist, then I want a date in the future so that I can do date range queries. To do this, I made a tweak of the above:

LOAD CSV WITH HEADERS FROM 'file:///myfile.csv' AS line
CREATE (n:Node {value:line.Value, fromDate:line.FromDate})
FOREACH (i IN CASE WHEN line.ToDate IS NOT NULL THEN [line.ToDate] ELSE ['2099-12-31'] END | SET n.toDate = i)

--

--

Ljubica Lazarevic
Ljubica Lazarevic

Written by Ljubica Lazarevic

Technologist — data geek — solver of problems

No responses yet