# String type conversion to date type

**URL:** https://discuss.elastic.co/t/string-type-conversion-to-date-type/245698
**Category:** Kibana
**Created:** [August 20, 2020, 4:02am UTC](https://discuss.elastic.co/t/string-type-conversion-to-date-type/245698 "2020-08-20T04:02:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Rachana\_Pole](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rachana_pole/32/49699_2.png) [@Rachana\_Pole](https://discuss.elastic.co/u/Rachana_Pole)
#### Post date: [August 20, 2020, 4:02am UTC](https://discuss.elastic.co/t/string-type-conversion-to-date-type/245698/1 "2020-08-20T04:02:49Z")

</div>

Hi all,  
I have a field that provides the date information but is coming is of String Data Type.  
Ex: metrics.database.state.my\_session\_time 2020-Aug-19 04:40:53  
I want to convert it into a date format. I tried a few date mapping formats from _[https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html)_ but was not able to convert. i also tried using scripted fields as suggested on _[Converting a string date to a Date field using scripted fields in kibana](https://discuss.elastic.co/t/converting-a-string-date-to-a-date-field-using-scripted-fields-in-kibana/108952/2)_ but it threw the following error:

```auto
      {
    "root_cause": [
     {
      "type": "script_exception",
      "reason": "runtime error",
      "script_stack": [
       "java.base/java.text.DateFormat.parse(DateFormat.java:395)",
       "new SimpleDateFormat('yyyy-MMM-dd HH mm ss').parse(doc['metrics.database.state.my_session_time.keyword'].value).getTime();",
       " ^---- HERE"
      ],
      "script": "new SimpleDateFormat('yyyy-MMM-dd HH mm ss').parse(doc['metrics.database.state.my_session_time.keyword'].value).getTime();",
      "lang": "painless"
     }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
     {
      "shard": 0,
      "index": "nw-conc",
      "node": "BR7csYvwSLyw5aSg7KCTZwq223",
      "reason": {
       "type": "script_exception",
       "reason": "runtime error",
       "script_stack": [
        "java.base/java.text.DateFormat.parse(DateFormat.java:395)",
        "new SimpleDateFormat('yyyy-MMM-dd HH mm ss').parse(doc['metrics.database.state.my_session_time.keyword'].value).getTime();",
        " ^---- HERE"
       ],
       "script": "new SimpleDateFormat('yyyy-MMM-dd HH mm ss').parse(doc['metrics.database.state.my_session_time.keyword'].value).getTime();",
       "lang": "painless",
       "caused_by": {
        "type": "parse_exception",
        "reason": "Unparseable date: \"\""
       }
      }
     }
    ]
   }

```

Can somebody please help me here? Thanks in adv

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [August 20, 2020, 8:26am UTC](https://discuss.elastic.co/t/string-type-conversion-to-date-type/245698/2 "2020-08-20T08:26:58Z")

</div>

For your scripted fields problem:

The "reason" field in these error messages is always the most helpful:

> [@Rachana\_Pole](#):
>
> ```auto
> "reason": "Unparseable date: \"\""
>  
> 
> ```

It seems like your data contains documents which don't include `metrics.database.state.my_session_time.keyword` - this can be fixed for example by checking whether that's the case before attempting to parse and returning null if it's an empty string (like in this case). Your scripted field has to be able to successfully run for all documents, otherwise your search requests will fail.

That being said, solving this in the mapping is a much better approach (especially for performance reasons). If you have trouble setting it up correctly, folks in the Elasticsearch forum will be happy to help: [Elasticsearch - Discuss the Elastic Stack](https://discuss.elastic.co/c/elastic-stack/elasticsearch/6) Please post your question over there as well.

---

<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: [September 17, 2020, 8:27am UTC](https://discuss.elastic.co/t/string-type-conversion-to-date-type/245698/3 "2020-09-17T08:27:17Z")

</div>

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