# Kibana create script field : concatenated strings with IF conditions

**URL:** <https://discuss.elastic.co/t/kibana-create-script-field-concatenated-strings-with-if-conditions/304955>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [May 17, 2022, 2:53pm UTC](https://discuss.elastic.co/t/kibana-create-script-field-concatenated-strings-with-if-conditions/304955 "2022-05-17T14:53:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arthur\_Parkopoly](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arthur_parkopoly/32/37867_2.png) [@Arthur\_Parkopoly](https://discuss.elastic.co/u/Arthur_Parkopoly)\
**Post date:** [May 17, 2022, 2:53pm UTC](https://discuss.elastic.co/t/kibana-create-script-field-concatenated-strings-with-if-conditions/304955/1 "2022-05-17T14:53:28Z")

</div>

Hello to the community,

I try to combine 2 date fields into a more meaning ful STRING scripted field based on conditions on the date to add "0" before 1 digit numbers ( May 2nd =\> 05-02).

This script is refused and I don't know how to solve this even after significant searches on forums  
Could you please help?  
I work with Elasticsearch 7.8  
Thanks

```auto
if (doc['startDate'].value.getMonthValue() < 10) return ( 'Le 0'); else ('Le ') + doc['startDate'].value.getMonthValue() +
if (doc['startDate'].value.getDayOfMonth() < 10) return ( ' - 0'); else (' - ') + doc['startDate'].value.getDayOfMonth() +
if (doc['startDate'].value.getHour() < 10) return (' de 0'); else (' de ') + doc['startDate'].value.getHour() +
if (doc['endDate'].value.getHour() < 10) return ('h à 0'); else ('h à ') + doc['endDate'].value.getHour() +'h'

```

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [May 17, 2022, 4:06pm UTC](https://discuss.elastic.co/t/kibana-create-script-field-concatenated-strings-with-if-conditions/304955/2 "2022-05-17T16:06:42Z")

</div>

Hello, can you post a few examples on what you're trying to achieve? I'm trying to figure out if you can achieve this with field formatting or anything else.

---

<div class="post-metadata">

**Author:** ![Arthur\_Parkopoly](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arthur_parkopoly/32/37867_2.png) [@Arthur\_Parkopoly](https://discuss.elastic.co/u/Arthur_Parkopoly)\
**Post date:** [May 17, 2022, 7:48pm UTC](https://discuss.elastic.co/t/kibana-create-script-field-concatenated-strings-with-if-conditions/304955/3 "2022-05-17T19:48:54Z")

</div>

thanks !  
Indeed, formatting would be a solution, but I couldn't figure out how to do it,

```auto
[
 {
  "_id": "6148f71866c81e1752a9648aAntoine Renaud",
  "endDate": "2021-09-25T22:04:00.000Z",
  "startDate": "2021-09-25T22:00:00.000Z",
  "shfit name with date": [
   "Le 25 de 22h à 22h"
  ]
 },
 {
  "_id": "6148f71866c81e1752a9648aGregory Saint Etienne",
  "endDate": "2021-09-05T03:04:00.000Z",
  "startDate": "2021-09-05T02:00:00.000Z",
  "shfit name with date": [
   "Le 5 de 2h à 3h"    
  ]
 }]

```

I'd like to change the second one to "Le 05 de 02h à 03h" so that I can sort these new string fields by alphabetical order. The idea is to get the start date day and hour and concatenate with the end date hour. I also need to add the month first.

This is the current code

```auto
'Le '+doc['startDate'].value.getDayOfMonth()+' de '+doc['startDate'].value.getHour()+ 'h à ' + doc['endDate'].value.getHour()+'h'

```

thanks a lot

---

<div class="post-metadata">

**Author:** ![Arthur\_Parkopoly](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arthur_parkopoly/32/37867_2.png) [@Arthur\_Parkopoly](https://discuss.elastic.co/u/Arthur_Parkopoly)\
**Post date:** [May 18, 2022, 4:16pm UTC](https://discuss.elastic.co/t/kibana-create-script-field-concatenated-strings-with-if-conditions/304955/4 "2022-05-18T16:16:40Z")

</div>

@Marius_Dragomir do you have enough to check with above data?  
I would greatly appreciate it!  
thanks

---

<div class="post-metadata">

**Author:** ![Arthur\_Parkopoly](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arthur_parkopoly/32/37867_2.png) [@Arthur\_Parkopoly](https://discuss.elastic.co/u/Arthur_Parkopoly)\
**Post date:** [May 24, 2022, 2:58pm UTC](https://discuss.elastic.co/t/kibana-create-script-field-concatenated-strings-with-if-conditions/304955/5 "2022-05-24T14:58:35Z")

</div>

Someone shared the solution, here it is:

```auto
DecimalFormat df = new DecimalFormat("00");
'Le ' + df.format(doc['startDate'].value.getDayOfMonth()) + ' de ' + df.format(doc['startDate'].value.getHour()) + 'h à ' + df.format(doc['endDate'].value.getHour()) + 'h'

```

---

<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:** [June 21, 2022, 2:59pm UTC](https://discuss.elastic.co/t/kibana-create-script-field-concatenated-strings-with-if-conditions/304955/6 "2022-06-21T14:59:34Z")

</div>

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