Intermediate Cypher-3

 In Cypher (Neo4j) you can work with strings, dates, and numbers using built-in functions, just like in SQL or Python.


🔹 1. Working with Strings

Cypher has many string functions.

Examples:

// Convert to uppercase RETURN toUpper("neo4j") // "NEO4J" // Convert to lowercase RETURN toLower("GRAPH") // "graph" // Substring (start, length) RETURN substring("GraphDatabase", 0, 5) // "Graph" // String length RETURN size("Neo4j") // 5 // Concatenation RETURN "Hello " + "World" // "Hello World" // Trim spaces RETURN trim(" Alice ") // "Alice"

🔹 2. Working with Dates & Times

Neo4j has temporal types: date, time, datetime, localdatetime, duration.

Examples:

// Current date and datetime RETURN date() // e.g., 2025-08-16 RETURN datetime() // e.g., 2025-08-16T21:30:00.123Z // Specific date RETURN date("2023-05-01") // Extract parts RETURN date().year, date().month, date().day // Add duration RETURN date("2023-05-01") + duration({months: 3}) // → 2023-08-01 // Difference RETURN duration.between(date("2023-01-01"), date("2023-08-01")) // → P7M (7 months)

🔹 3. Working with Numbers

Cypher supports math functions.

Examples:

// Basic math RETURN 5 + 3, 10 - 2, 4 * 2, 8 / 2 // Power & square root RETURN pow(2, 3), sqrt(16) // 8, 4 // Absolute value, round, ceil, floor RETURN abs(-7), round(3.6), ceil(3.2), floor(3.8) // → 7, 4, 4, 3 // Random number RETURN rand() // 0.0 ≤ n < 1.0

🔹 4. Mixing Types

You can combine these in queries.

Example:

MATCH (u:User) RETURN u.name, toUpper(u.name) AS upper_name, date().year - u.birthYear AS age

👉 Shows each user’s name, uppercase version, and age (calculated from birth year).


✨ Tip: Neo4j functions are well-documented here.

this is very important in Neo4j because using parameters makes queries faster, reusable, and safer (avoids injection attacks).


🔹 1. What are Parameters in Cypher?

Instead of hardcoding values in your query, you use placeholders like $paramName and supply values separately.

✅ Benefits:

  • Query plan can be cached → faster execution.

  • Cleaner queries.

  • Avoids string concatenation (safer).


🔹 2. Basic Example

Without parameter ❌ (hardcoded):

MATCH (u:User {name: "Alice"}) RETURN u

With parameter ✅:

MATCH (u:User {name: $username}) RETURN u

Supplying parameter (example in Neo4j Browser):

:param username => "Alice"

Or in code (Python driver):

session.run("MATCH (u:User {name: $username}) RETURN u", {"username": "Alice"})

🔹 3. Multiple Parameters

MATCH (u:User {name: $username, role: $role}) RETURN u

Set parameters:

:param username => "Bob" :param role => "Developer"

🔹 4. Parameters in Relationships

MATCH (a:User {name: $user})-[r:WORKS_ON]->(p:Project {name: $project}) RETURN a, r, p
:param user => "Alice" :param project => "ProjectX"

🔹 5. Parameters with Collections

MATCH (u:User) WHERE u.name IN $names RETURN u
:param names => ["Alice", "Bob", "Charlie"]

🔹 6. Parameters with Maps

You can even pass property maps:

CREATE (u:User $props)
:param props => {name: "Alice", role: "Admin", age: 30}

✅ Summary:

  • $param → placeholder in query.

  • Values supplied separately (:param in browser, dictionary in drivers).

  • Works with scalars, lists, and maps.

  • Improves performance & security.


Comments