cancel
Showing results for 
Search instead for 
Did you mean: 

LOAD CSV & CASE

proedig
Node

I would like to use LOAD CSV together with CASE. Depending on a value in one of the columns of each line, nodes with varying label should be created. In my example, I am dealing with two labels ("Input" and "Variable"). I tried the following:

LOAD CSV WITH HEADERS FROM 'file:///nodes.csv' AS line
CASE line.Typ
WHEN 'Input' THEN MERGE (n0:Input {name: line.Name})
WHEN 'Variable' THEN MERGE (n0:Variable {name: line.Name})
END
 
Any suggestions how to solve this task? Error message is 
Neo.ClientError.Statement.SyntaxError: Invalid input 'CASE'
1 REPLY 1

glilienfield
Ninja
Ninja

The case statement does not provide that functionality. It is not a control statement, but a method that returns a value. 

You have a number of options. You can use the apoc.do.when procedure to do what you tried to do with the case statement. 

https://neo4j.com/labs/apoc/4.4/overview/apoc.do/apoc.do.when/

You can use apoc.create.node to create the nodes. This method allows you to pass the label(s) to use. 

https://neo4j.com/labs/apoc/4.3/overview/apoc.create/apoc.create.node/

Another option using pure cypher is to use a series of call subqueries to create the nodes, with one call subquery for each different condition. Here is a code snippet to show you the idea:  

call{
with line
with line, line.Typ as type
where type = “Input”
MERGE (:Input {name: line.Name})
}
call
{
with line
with line, line.Typ = “Variable”
MERGE (:Variable {name: line.Name})
}

 

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.