title: Neo4j: Conditional return/IF clause/String manipulation
body: <p>This is in continuation of <a href="https://stackoverflow.com/questions/28681685/neo4j-listing-node-labels">Neo4j: Listing node labels</a></p>
  
  <p>I am constructing a dynamic MATCH statement to return the hierarchy structure &amp; use the output as a Neo4j JDBC input to query the data from a java method:</p>
  
  <pre><code>MATCH p=(:Service)&lt;-[*]-(:Anomaly)
  WITH head(nodes(p)) AS Service, p, count(p) AS cnt
  RETURN DISTINCT Service.company_id, Service.company_site_id,
  "MATCH srvhier=(" +
     reduce(labels = "", n IN nodes(p) | labels + labels(n)[0] +  
      "&lt;-[:BELONGS_TO]-") + ") WHERE Service.company_id = {1} AND
      Service.company_site_id = {2} AND Anomaly.name={3} RETURN " + 
      reduce(labels = "", n IN nodes(p) | labels + labels(n)[0] + ".name,");
  </code></pre>
  
  <p>The output is as follows:</p>
  
  <pre><code>MATCH srvhier=(Service&lt;-[:BELONGS_TO]-Category&lt;-[:BELONGS_TO]-SubService&lt;-
  [:BELONGS_TO]-Assets&lt;-[:BELONGS_TO]-Anomaly&lt;-[:BELONGS_TO]-) WHERE 
  Service.company_id = {1} and Service.company_site_id = {21} and 
  Anomaly.name={3} RETURN Service.name, Category.name, SubService.name, 
  Assets.name, Anomaly.name,
  </code></pre>
  
  <p>The problem I am seeing:</p>
  
  <p>The "BELONGS_TO" gets appended to my last node</p>
  
  <p>Line 2: Assets&lt;-[:BELONGS_TO]-Anomaly**&lt;-[:BELONGS_TO]-**</p>
  
  <p>Are there string functions (I have looked at Substring..) that can be used to remove it? Or can I use a CASE statement with condition n=cnt to append "BELONGS_TO"?</p>
  
  <p>The same problem persists with my last line:</p>
  
  <p>Line 5: Assets.name,Anomaly.name**,** - the additional "," that I need to eliminate.</p>
  
  <p>Thanks.</p>