# Issues while using SQL queries with curl

**URL:** https://discuss.elastic.co/t/issues-while-using-sql-queries-with-curl/223118
**Category:** Elasticsearch
**Created:** [March 11, 2020, 11:47am UTC](https://discuss.elastic.co/t/issues-while-using-sql-queries-with-curl/223118 "2020-03-11T11:47:40Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tarund](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tarund](https://discuss.elastic.co/u/tarund)
#### Post date: [March 11, 2020, 11:47am UTC](https://discuss.elastic.co/t/issues-while-using-sql-queries-with-curl/223118/1 "2020-03-11T11:47:41Z")

</div>

Hi there,

I am trying to export data out of Elasticsearch into csv files using SQL queries through curl. Most of the basic queries work fine, but when I try to use LIKE operator or CASE WHEN operators, I get exceptions. Same queries work fine from Kibana dev console.  
Can you please help ?  
Please find below queries & exceptions recvd -

1. curl -XGET "[http://20.62.27.19:9200/\_sql?format=csv](http://20.62.27.19:9200/_sql?format=csv)" -H 'Content-Type: application/json' -d'{ "query": "\n SELECT HISTOGRAM("@timestamp", INTERVAL 1 HOUR) AS t,COUNT(\*) AS count\n FROM "rtransactions"\n WHERE event LIKE "mp%trof%"\n GROUP BY t\n " }'

"error":{"root\_cause":[{"type":"parsing\_exception","reason":"line 1:152: mismatched input '"mp%trof%"' expecting {'?', STRING}"}],"type":"parsing\_exception","reason":"line 1:152: mismatched input '"mp%trof%"' expecting {'?', STRING}","caused\_by":{"type":"input\_mismatch\_exception","reason":null}},"status":400}

---

<div class="post-metadata">

### Author: ![matriv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matriv/32/43656_2.png) [@matriv](https://discuss.elastic.co/u/matriv)
#### Post date: [March 11, 2020, 3:07pm UTC](https://discuss.elastic.co/t/issues-while-using-sql-queries-with-curl/223118/2 "2020-03-11T15:07:48Z")

</div>

You need single quotes around the string literals and you need to escape them properly:

```auto
curl -XGET "http://20.62.27.19:9200/_sql?format=csv" -H 'Content-Type: application/json' -d'{ "query": "SELECT HISTOGRAM(\"@timestamp\", INTERVAL 1 HOUR) AS t,COUNT(*) AS count FROM \"rtransactions\" WHERE event LIKE '"'"'mp%trof%'"'"' GROUP BY t" }'

```

---

<div class="post-metadata">

### Author: ![tarund](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tarund](https://discuss.elastic.co/u/tarund)
#### Post date: [March 12, 2020, 6:34am UTC](https://discuss.elastic.co/t/issues-while-using-sql-queries-with-curl/223118/3 "2020-03-12T06:34:55Z")

</div>

Thanks @matriv. it worked. I had tried both single & double quotes but I was using \ to escape.  
Can you help me understand the syntax for escaping string literals.  
I tried your solution inside a CASE WHEN clause, but that didn't work. It didn't throw an error, but the docs were not filtered and went in the ELSE block.

---

<div class="post-metadata">

### Author: ![matriv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matriv/32/43656_2.png) [@matriv](https://discuss.elastic.co/u/matriv)
#### Post date: [March 12, 2020, 11:18am UTC](https://discuss.elastic.co/t/issues-while-using-sql-queries-with-curl/223118/4 "2020-03-12T11:18:37Z")

</div>

If it was SQL statement on the CLI for example it would simply be:

```auto
SELECT HISTOGRAM("@timestamp", INTERVAL 1 HOUR) AS t,COUNT(*) AS count FROM "rtransactions" WHERE event LIKE 'mp%trof%' GROUP BY t

```

Now since you are inside a json document you have to escape the `"` as `\"`.  
The single quotes wouldn't need escaping but because you are in the shell and you have an open `'` for the body you have to escape them as shown.

---

<div class="post-metadata">

### Author: ![tarund](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tarund](https://discuss.elastic.co/u/tarund)
#### Post date: [March 12, 2020, 1:30pm UTC](https://discuss.elastic.co/t/issues-while-using-sql-queries-with-curl/223118/5 "2020-03-12T13:30:35Z")

</div>

But it still doesn't work inside a CASE WHEN clause. It didn't throw an error, but the docs were not filtered and went in the ELSE block. Please suggest what am I missing ?

curl -XGET "[http://20.62.27.19:9200/\_sql?format=csv](http://20.62.27.19:9200/_sql?format=csv)" -H 'Content-Type: application/json' -d'{ "query": " SELECT HISTOGRAM("@timestamp", INTERVAL 1 HOUR) AS t,COUNT(\*) AS count, CASE WHEN labels.event LIKE '"'"'c%ato%'"'"' THEN 1 WHEN labels.event LIKE '"'"'c%rot%'"'"' THEN 2 ELSE 4 END AS type FROM "atransaction" GROUP BY t,type " }'

---

<div class="post-metadata">

### Author: ![matriv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matriv/32/43656_2.png) [@matriv](https://discuss.elastic.co/u/matriv)
#### Post date: [March 12, 2020, 3:10pm UTC](https://discuss.elastic.co/t/issues-while-using-sql-queries-with-curl/223118/6 "2020-03-12T15:10:42Z")

</div>

@tarund,

- You still need to escape the `"` around `@timestamp` and `atransaction` with `\`.
- There is a [bug](https://github.com/elastic/elasticsearch/issues/53486) regarding the usage of LIKE inside another scalar function (in your case: CASE WHEN ... ELSE ... END).

Thanks for catching that!

---

<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: [April 9, 2020, 3:13pm UTC](https://discuss.elastic.co/t/issues-while-using-sql-queries-with-curl/223118/7 "2020-04-09T15:13:17Z")

</div>

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