# Cusom date query

**URL:** <https://discuss.elastic.co/t/cusom-date-query/148778>\
**Category:** Elasticsearch\
**Created:** [September 17, 2018, 7:30am UTC](https://discuss.elastic.co/t/cusom-date-query/148778 "2018-09-17T07:30:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [September 17, 2018, 7:30am UTC](https://discuss.elastic.co/t/cusom-date-query/148778/1 "2018-09-17T07:30:27Z")

</div>

Any idea how to transform this query to elasticsearch ? (myDate is a date format coming from the datatabse, we're just transforming it here to a year month only format)

```
SELECT date_format(myDate,'%Y%m') as custom
FROM table
GROUP BY date_format(myDate,'%Y%m') 
ORDER BY date_format(myDate,'%Y%m')

```

In other words, how to group by a custom date? How to use the terms agg with a script to group by YYYY-MM rather than YYYY-MM-DD?

Or can I use something else like `date_histogram` to format a YYY-MM-DD date to YYYY-MM ?

---

<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:** [October 15, 2018, 7:30am UTC](https://discuss.elastic.co/t/cusom-date-query/148778/2 "2018-10-15T07:30:28Z")

</div>

Closed on October 15, 2018, 7:30am UTC

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