# How to use value from elasticsearch index as parameter in ES SQL functions, like DATE\_DIFF()

**URL:** https://discuss.elastic.co/t/how-to-use-value-from-elasticsearch-index-as-parameter-in-es-sql-functions-like-date-diff/242365
**Category:** Elasticsearch
**Tags:** elastic-stack-sql
**Created:** [July 23, 2020, 3:30pm UTC](https://discuss.elastic.co/t/how-to-use-value-from-elasticsearch-index-as-parameter-in-es-sql-functions-like-date-diff/242365 "2020-07-23T15:30:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![burakka](https://avatars.discourse-cdn.com/v4/letter/b/a5b964/32.png) [@burakka](https://discuss.elastic.co/u/burakka)
#### Post date: [July 23, 2020, 3:30pm UTC](https://discuss.elastic.co/t/how-to-use-value-from-elasticsearch-index-as-parameter-in-es-sql-functions-like-date-diff/242365/1 "2020-07-23T15:30:27Z")

</div>

Hello guys!

I want to use Elastic SQL with functions in Kibana Canvas. Therefore I tried to use the function DATE\_DIFF(), with values from an existing Elasticsearch index as parameter in the sql function.

I looked at the example from the documentation, but the examples are with concrete values as string:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-functions-datetime.html#sql-functions-datetime-diff](https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-functions-datetime.html#sql-functions-datetime-diff)

I tested this query with an subquery in Kibana Dev Tools Console but I got an error.

```auto
POST /_sql?format=txt
{
  "query":"SELECT DATE_DIFF('days', (SELECT timestamp1 FROM index_name WHERE something = 'Lorem')::datetime, '2020-12-04'::date)"

```

Error:

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "unsupported_operation_exception",
        "reason" : null
      }
    ],
    "type" : "unsupported_operation_exception",
    "reason" : null
  },
  "status" : 500
}

```

Thank you for your help!!

Kind regards.

---

<div class="post-metadata">

### Author: ![bogdan.pintea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bogdan.pintea/32/45740_2.png) [@bogdan.pintea](https://discuss.elastic.co/u/bogdan.pintea)
#### Post date: [July 27, 2020, 7:59am UTC](https://discuss.elastic.co/t/how-to-use-value-from-elasticsearch-index-as-parameter-in-es-sql-functions-like-date-diff/242365/2 "2020-07-27T07:59:26Z")

</div>

Hello @burakka,  
You might want to rephrase your query like:

```auto
SELECT DATE_DIFF('days', timestamp1, '2020-12-04'::date) FROM index_name WHERE something = 'Lorem'

```

.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [August 25, 2020, 12:05am UTC](https://discuss.elastic.co/t/how-to-use-value-from-elasticsearch-index-as-parameter-in-es-sql-functions-like-date-diff/242365/4 "2020-08-25T00:05:20Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
