> For the complete documentation index, see [llms.txt](https://gchandra.gitbook.io/big-data-and-tools-with-nosql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gchandra.gitbook.io/big-data-and-tools-with-nosql/nosql/neo4j/examples/commonly-used-functions.md).

# Commonly used Functions

### String Functions

```cypher
match (a) return toUpper(a.firstname);


RETURN left('hello', 3)

RETURN lTrim('   hello')

RETURN replace("hello", "l", "w")

RETURN reverse('hello')

RETURN substring('hello', 1, 3), substring('hello', 2)


RETURN toString(11.5),
toString('already a string'),
toString(true),
toString(date({year:1984, month:10, day:11})) AS dateString,
toString(datetime({year:1984, month:10, day:11, hour:12, minute:31, second:14, millisecond: 341, timezone: 'Europe/Stockholm'})) AS datetimeString,
toString(duration({minutes: 12, seconds: -60})) AS durationString

```

#### Aggregation Functions

* **`COUNT`**: Counts the number of items.

  ```cypher
  MATCH (n:Student)
  RETURN COUNT(n);
  ```
* **`MAX`** and **`MIN`**: Find the maximum or minimum of a set of values.

  ```cypher
  MATCH (n:Student)-[r:TAKING]->(c)
  RETURN MAX(r.grade), MIN(r.grade);
  ```

#### Date and Time Functions

* **`date()`**: Creates a date from a string or a map.

  ```cypher
  RETURN date('2024-03-20');
  ```
* **`datetime()`**: Creates a datetime from a string or a map.

  ```cypher
  RETURN datetime('2024-03-20T12:00:00');
  ```
* **`duration.between()`**: Calculates the duration between two temporal values.

  ```cypher
  RETURN duration.between(date('1984-10-11'), date('2024-03-20'));
  ```

#### List Functions

* **`COLLECT`**: Aggregates values into a list.

  ```cypher
  MATCH (n:Student)
  RETURN COLLECT(n.firstname);
  ```
* **`SIZE`**: Returns the size of a list.

  ```cypher
  MATCH (n:Student)
  RETURN COLLECT(n.firstname), SIZE(COLLECT(n.firstname));
  ```
* **`RANGE`**: Creates a list containing a sequence of integers.

  ```cypher
  RETURN RANGE(1, 10, 2);
  ```

#### Mathematical Functions

* **`ABS`**: Returns the absolute value.

  ```cypher
  RETURN ABS(-42);
  ```
* **`ROUND`**, **`CEIL`**, **`FLOOR`**: Round numbers to the nearest integer, up, or down.

  ```cypher
  RETURN ROUND(3.14159), CEIL(3.14159), FLOOR(3.14159);
  ```

#### Logical Functions

* **`COALESCE`**: Returns the first non-null value in a list of expressions.

  ```cypher
  RETURN COALESCE(NULL, 'first non-null', NULL);
  ```

#### Spatial Functions

* **`point`**: Creates a point in a 2D space (or 3D if you add elevation) which can be used for spatial queries.

  ```cypher
  RETURN point({latitude: 37.4847, longitude: -122.148})
  ```
* **`distance`**: Calculates the geodesic distance between two points in Meters.  Cherryhill to Moorestown. Verify the result  <https://www.nhc.noaa.gov/gccalc.shtml>

  ```cypher
  RETURN point.distance(
    point({latitude: 39.94, longitude: -75.01}), 
    point({latitude: 39.97, longitude: -74.96})
  ) / 1609.34 AS distanceInMiles
  ```
* **`apoc.coll.sort`**: Sorts a list.

  ```cypher
  RETURN apoc.coll.sort(['banana', 'apple', 'cherry'])
  ```
* **`apoc.map.merge`**: Merges two maps.

  ```cypher
  RETURN apoc.map.merge({name: 'Neo'}, {age: 23})
  ```

<br>
