# AQL query using function SUBSTRING

**URL:** https://discourse.openehr.org/t/aql-query-using-function-substring/5412
**Category:** AQL
**Tags:** aql, template
**Created:** [28 June 2024 13:39 UTC](https://discourse.openehr.org/t/aql-query-using-function-substring/5412 "2024-06-28T13:39:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Lucas](https://discourse.openehr.org/user_avatar/discourse.openehr.org/lucas/32/3185_2.png) [@Lucas](https://discourse.openehr.org/u/Lucas)
#### Post date: [28 June 2024 13:39 UTC](https://discourse.openehr.org/t/aql-query-using-function-substring/5412/1 "2024-06-28T13:39:45Z")

</div>

My current situation:  
Based on the [specification for the Archetype Query Languange for SUBSTRING](https://specifications.openehr.org/releases/QUERY/latest/AQL.html#_substring) I am testing a possibility how to perform a query by using the SUBSTRING of the attribute “name” of the template and comparing it with a defined String.

My goal:  
Every score in our electronic health record may have a different templateID. For this purpose, I want to analyze the performance of AQL queries based on a naming convention, where all these Scores might start with “Score” in the attribute name of the template. Consequently I am trying to apply the following query

```auto
{
  "q": "SELECT c FROM COMPOSITION c WHERE SUBSTRING(c/name/value,0,5)='Score'",
  "offset": 0
}

```

My questions:

1. Do Better’s openEHR database or Vitagroup’s EHRBase support the substring function?
2. How do we correctly apply the substring function to successfully perform the example query above based on the REST API endpoint for /query/aql?

---

<div class="post-metadata">

### Author: ![ian.mcnicoll](https://discourse.openehr.org/user_avatar/discourse.openehr.org/ian.mcnicoll/32/4430_2.png) [@ian.mcnicoll](https://discourse.openehr.org/u/ian.mcnicoll)
#### Post date: [28 June 2024 13:58 UTC](https://discourse.openehr.org/t/aql-query-using-function-substring/5412/2 "2024-06-28T13:58:16Z")

</div>

both support LIKE. I’m not sure about SUBSTRING

Better EhrServer

> Partial matching with `LIKE` operator
> 
> Allows matching with wildcards `*` and `?`:
> 
> - `*` stands for _any string_,
> - `?` stands for _any character_.
> 
> ```auto
> SELECT DISTINCT c/archetype_details/template_id/value as templateName
> FROM EHR e
> CONTAINS COMPOSITION c
> WHERE templateName LIKE "Vital*"
> 
> ```

EhrBase (2)

> ## LIKE[​](https://docs.ehrbase.org/docs/EHRbase/Explore/AQL/Where#like)
> 
> The `LIKE` binary operator is used to compare a value of type string (or dates and times) against a simple pattern. The left-hand operand is an AQL identified path to a data element that is either a String or has a String representation, while the right-hand operand is a String value, representing the pattern to be matched. It returns true if the value matches the supplied pattern.
> 
> Below is an example using a simple pattern matching:
> 
> ```auto
> SELECT e/ehr_id/value, c/context/start_timeFROM EHR e CONTAINS COMPOSITION c[openEHR-EHR-COMPOSITION.administrative_encounter.v1] CONTAINS ADMIN_ENTRY admission[openEHR-EHR-ADMIN_ENTRY.admission.v1]WHERE c/context/start_time LIKE '2019-0?-*'
> 
> ```

---

<div class="post-metadata">

### Author: ![borut.jures](https://discourse.openehr.org/user_avatar/discourse.openehr.org/borut.jures/32/1391_2.png) [@borut.jures](https://discourse.openehr.org/u/borut.jures)
#### Post date: [28 June 2024 13:58 UTC](https://discourse.openehr.org/t/aql-query-using-function-substring/5412/3 "2024-06-28T13:58:52Z")

</div>

The more precise template name is in the `archetype_details.template_id.value`.

Edit: Ian and I posted at the same time. Just follow the Ian’s answer.

---

<div class="post-metadata">

### Author: ![Lucas](https://discourse.openehr.org/user_avatar/discourse.openehr.org/lucas/32/3185_2.png) [@Lucas](https://discourse.openehr.org/u/Lucas)
#### Post date: [28 June 2024 14:11 UTC](https://discourse.openehr.org/t/aql-query-using-function-substring/5412/4 "2024-06-28T14:11:24Z")

</div>

Thank you both so much for your prompt answer. Now I understand, why I was unable to perform this request as intended. I tried to use “%” and “\_” as wildcards as in SQL.

Concerning the SUBSTRING function in AQL queries, is it implemented? And if so, how do we correctly apply it?

---

<div class="post-metadata">

### Author: ![ian.mcnicoll](https://discourse.openehr.org/user_avatar/discourse.openehr.org/ian.mcnicoll/32/4430_2.png) [@ian.mcnicoll](https://discourse.openehr.org/u/ian.mcnicoll)
#### Post date: [28 June 2024 14:46 UTC](https://discourse.openehr.org/t/aql-query-using-function-substring/5412/5 "2024-06-28T14:46:37Z")

</div>

SUBSTRING() is not documented as being available in either CDR but your interpretation of the openEHR docs looks correct to me.

---

<div class="post-metadata">

### Author: ![Lucas](https://discourse.openehr.org/user_avatar/discourse.openehr.org/lucas/32/3185_2.png) [@Lucas](https://discourse.openehr.org/u/Lucas)
#### Post date: [1 July 2024 07:21 UTC](https://discourse.openehr.org/t/aql-query-using-function-substring/5412/6 "2024-07-01T07:21:34Z")

</div>

Thank you, Ian, for your feedback and clarification on the function. But fortunately, you provided a better solution than the substring, such that I was able to test it.
