Search code examples
stringneo4jtrim

using conditional in CASE does not evaluate expressions properly ? neo4j


I would like to trim strings server side.

Consider this: a property is string, and I want to trim it if longer than, say, 10 chars. Consider this query:

Match (r)
with r, case r.what
    when size(r.what) > 10 then substring(r.what, 0, 10)
    else r.what end as w
return {what : w, len : size(w)} as props 

I expect size(r.what) > 10 evaluate to true and substring(r.what, 0, 10) assigned to variable w, w is always equal to the original string 'r.what', no matter of length. len is reported correctly.

What am I doing wrong? Im using neo4j 4.4


Solution

  • Remove the r.what after the CASE. Your query should look like this:

    MATCH (r)
    WITH r,
         CASE
           WHEN size(r.what) > 10 THEN substring(r.what, 0, 10)
           else r.what
         END AS w
    RETURN {what : w, len : size(w)} AS props 
    

    When you add an expression after the CASE (in your problem, the r.what), its value will be compared to whatever you put on each WHEN.

    Sources: