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.

What's the issue of my cypher to remove trailing spaces?

lingvisa
Graph Fellow
MATCH (n) 
WITH n.Name as name
WHERE name ENDS WITH ' '
SET name = rTrim(name)
return name

I want to find nodes whose Name has trailing space and remove that space, but the command gives this error:

Type mismatch: expected Map, Node or Relationship but was String (line 4, column 12 (offset: 73))
"SET name = rTrim(name)"

The 'name' should be a string. Why does the set expects a map, node or relationship?

1 ACCEPTED SOLUTION

@lingvisa

Neo4j version? if this is a bug I'd like to test/reproduce against the same version you have encountered with

But also

MATCH (n:Product)
WITH n, CASE n.name when NOT NULL THEN replace(n.name,'[','') ELSE n.name END as newName
SET n.name = newName
RETURN count(*)

should accomplish what is desired

View solution in original post

8 REPLIES 8

Because the SET clause expect to modify a property's value. Properties must belongs to a node or relation to be modify. In your case, name is not a property anymore but just a string, so it can't be set.

What you want to do it's keep your node to modify it's property.

MATCH (n)
SET n.name = rTrim(n.name)

Note : In this case as Neo4j will have to filter all your nodes anyway to figure out which one ends with a trailing space, so you can just apply rTrim to all of them. Might be good to double check the performance if it's a production project.

It's highly recommended to keep in lower case all your properties name.

You don't need to return anything when you WRITE something but you can still return count(*). Neo4j Desktop will provide you some hits about your query result anyway.

@tard.gabriel This is a similar query and is invalid:

MATCH (n:Product)
with n.name as Name
set n.name = replace(Name, '☆', '')
return n.name

The error message is:

Variable `n` not defined (line 3, column 5 (offset: 38))
"set n.name = replace(Name, '☆', '')"

Can't have a With statement between Match and Set? How to achieve this purpose to modify the name property?

@lingvisa

the correct syntax is

MATCH (n:Product)
with n,n.name as Name
set n.name = replace(Name, '☆', '')
return n.name

or

MATCH (n:Product)
set n.name = replace(n.name, '☆', '')
return n.name

MATCH (n:Product)
WITH n, n.name as N
CASE WHEN N IS NOT NULL THEN replace(N,'[','') ELSE N END as newName
SET n.name = newName
RETURN count(*)

But I received this error:

Invalid input 'S': expected 'l/L' (line 3, column 3 (offset: 70))
"CASE WHEN ING IS NOT NULL THEN replace(ING,'[','') ELSE ING END as result"
   ^

@dana.canzano Another similar one. Why is the syntax wrong?

@lingvisa

Neo4j version? if this is a bug I'd like to test/reproduce against the same version you have encountered with

But also

MATCH (n:Product)
WITH n, CASE n.name when NOT NULL THEN replace(n.name,'[','') ELSE n.name END as newName
SET n.name = newName
RETURN count(*)

should accomplish what is desired

I initially tested in 4.0.4, then tested in 4.2.6, and the message is more informatvie as below. Perhaps it doesn't like "CASE" starts with a new cypher statement:

Invalid input 'CASE': expected 
  <EOF>
  "RETURN"
  "CREATE"
  "DELETE"
  "SET"
  "REMOVE"
  "DETACH"
  "MATCH"
  "WITH"
  "UNWIND"
  "USE"
  "CALL"
  "LOAD"
  "FROM"
  "FOREACH"
  "WHERE"
  "MERGE"
  "OPTIONAL"
  "ORDER"
  "SKIP"
  "LIMIT"
  "UNION"
  "," (line 3, column 1 (offset: 37))
"CASE N when NOT NULL THEN replace(n.name,'[','') ELSE n.name END as newName"
 ^

@lingvisa

Thanks for the version detail.

The cypher I provided did not fail. Is your last post an error from the cypher I had provided ? If not what cypher is now being submitted

Your cypher is good. The last error message was produced from 4.2.6 version, and I think it's more informative. Probably it doesn't like CASE starts a new statement.