# How to add a 0 before a field, with a condicion?

**URL:** <https://discuss.elastic.co/t/how-to-add-a-0-before-a-field-with-a-condicion/308106>\
**Category:** Logstash\
**Created:** [June 24, 2022, 11:25am UTC](https://discuss.elastic.co/t/how-to-add-a-0-before-a-field-with-a-condicion/308106 "2022-06-24T11:25:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvinacio](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@jvinacio](https://discuss.elastic.co/u/jvinacio)\
**Post date:** [June 24, 2022, 11:25am UTC](https://discuss.elastic.co/t/how-to-add-a-0-before-a-field-with-a-condicion/308106/1 "2022-06-24T11:25:57Z")

</div>

Hello!  
I have different docs with different hours from 0 to 23 like the following  
{  
"date" : "2022-06-23",  
"eventDate" : "2022",  
"StoreID" : 190,  
"hour" : 0,  
"PosID" : 12,  
"tempo" : 0.0  
}  
{  
"date" : "2022-06-23",  
"eventDate" : "2022",  
"StoreID" : 190,  
"hour" : 1,  
"PosID" : 12,  
"tempo" : 0.0  
}  
For the field hour, i need to add a 0 before the number if its below 10.  
I tried different methods like this:  
ruby {  
code =\> '   
if event.get('hour') =~ /^\d{1}$/  
event.set('hour', '0'+event.get('hour'))  
end   
'  
}  
but gives me an error  
What can I do?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [June 24, 2022, 3:52pm UTC](https://discuss.elastic.co/t/how-to-add-a-0-before-a-field-with-a-condicion/308106/2 "2022-06-24T15:52:34Z")

</div>

> [@jvinacio](#):
>
> code =\> '

If you use single quotes to surround the code then within the code you must use double quotes.

---

<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:** [July 22, 2022, 3:53pm UTC](https://discuss.elastic.co/t/how-to-add-a-0-before-a-field-with-a-condicion/308106/3 "2022-07-22T15:53:26Z")

</div>

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