<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to create correct format inside a multivalue field? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-correct-format-inside-a-multivalue-field/m-p/653501#M225835</link>
    <description>&lt;P&gt;You can do it like this&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| fields - _time
| eval outputdates=split("2023-07-29 12:06:20,28-07-2023 00:03:05,", ",")
``` This uses any number of strftime variants to parse the dates - the succeeding one will prevail ```
| eval outputdates=mvmap(outputdates, strftime(max(
                         strptime(outputdates, "%F %T"), 
                         strptime(outputdates, "%d-%m-%Y %T")), 
                                               "%F %T"))&lt;/LI-CODE&gt;&lt;P&gt;mvmap is used to iterate through the MV values. The first 3 lines set up your example&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 08 Aug 2023 05:57:55 GMT</pubDate>
    <dc:creator>bowesmana</dc:creator>
    <dc:date>2023-08-08T05:57:55Z</dc:date>
    <item>
      <title>How to create correct format inside a multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-correct-format-inside-a-multivalue-field/m-p/653408#M225801</link>
      <description>&lt;P&gt;Hello to all,&lt;/P&gt;
&lt;P&gt;I have a multivalue field with a date and also a null value. In addition I have the problem that the format of the date is twisted from the original data.&lt;/P&gt;
&lt;P&gt;Hope someone can help me &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;example:&lt;BR /&gt;&lt;BR /&gt;&lt;U&gt;outputdates (mv-field)&lt;BR /&gt;&lt;/U&gt;2023-07-29 12:06:20&lt;BR /&gt;28-07-2023 00:03:05&lt;BR /&gt;null&lt;BR /&gt;&lt;BR /&gt;needed result:&lt;BR /&gt;&lt;U&gt;outputdates (mv-field)&lt;/U&gt;&lt;BR /&gt;2023-07-29 12:06:20&lt;BR /&gt;2023-07-28 00:03:05&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;i tried splitting the field with mvexpand and then using strftime to adjust the format, unfortunately this is not working and i don't know exactly why.&lt;BR /&gt;Do I need to transform the "outputdates" as a time field to make strtime work again?&lt;/P&gt;
&lt;P&gt;Am any help grateful.&lt;BR /&gt;Many greetings,&lt;BR /&gt;Flenwy&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 19:26:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-correct-format-inside-a-multivalue-field/m-p/653408#M225801</guid>
      <dc:creator>Flenwy</dc:creator>
      <dc:date>2023-08-07T19:26:00Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk search, Correct format inside a multivalue field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-correct-format-inside-a-multivalue-field/m-p/653410#M225803</link>
      <description>&lt;P&gt;To convert textual dates from one format to another requires both &lt;FONT face="courier new,courier"&gt;strptime&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;strftime&lt;/FONT&gt;.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval outputdates=if(match(outputdates,"\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d"), outputdates, strftime(strptime(outputdates, "%d-%m-%Y %H:%M:%S"), "%Y-%m-%d %H:%M:%S"))&lt;/LI-CODE&gt;&lt;P&gt;Because &lt;FONT face="comic sans ms,sans-serif"&gt;strptime&lt;/FONT&gt; will return null if the input doesn't match the specified format, we use &lt;FONT face="courier new,courier"&gt;if&lt;/FONT&gt; to test for the format we want to convert.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 13:21:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-correct-format-inside-a-multivalue-field/m-p/653410#M225803</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2023-08-07T13:21:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to create correct format inside a multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-correct-format-inside-a-multivalue-field/m-p/653501#M225835</link>
      <description>&lt;P&gt;You can do it like this&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| fields - _time
| eval outputdates=split("2023-07-29 12:06:20,28-07-2023 00:03:05,", ",")
``` This uses any number of strftime variants to parse the dates - the succeeding one will prevail ```
| eval outputdates=mvmap(outputdates, strftime(max(
                         strptime(outputdates, "%F %T"), 
                         strptime(outputdates, "%d-%m-%Y %T")), 
                                               "%F %T"))&lt;/LI-CODE&gt;&lt;P&gt;mvmap is used to iterate through the MV values. The first 3 lines set up your example&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 05:57:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-correct-format-inside-a-multivalue-field/m-p/653501#M225835</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2023-08-08T05:57:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to create correct format inside a multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-correct-format-inside-a-multivalue-field/m-p/653528#M225843</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello Flenwy,&lt;/P&gt;&lt;P&gt;Certainly! It looks like you're dealing with dates in two different formats and also null values in a multivalue field. Here's a step-by-step solution to help you address the problem:&lt;/P&gt;&lt;P&gt;Identify the Format: You need to identify the format of each date string and then apply the necessary transformation. In your case, you have two formats: 'YYYY-MM-DD hh:mm:ss' and 'DD-MM-YYYY hh:mm:ss'.&lt;/P&gt;&lt;P&gt;Handle Null Values: Since you also have null values, you need to check for them before applying any transformations.&lt;/P&gt;&lt;P&gt;Transform the Dates: Depending on the identified format, you can then convert the date to the required format.&lt;/P&gt;&lt;P&gt;Here's a snippet of code that should help you achieve your goal:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;from datetime import datetime

outputdates = [
    "2023-07-29 12:06:20",
    "28-07-2023 00:03:05",
    None
]

result = []

for date in outputdates:
    if date is None:
        continue
    try:
        # If it's in 'YYYY-MM-DD' format
        parsed_date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
    except ValueError:
        # If it's in 'DD-MM-YYYY' format
        parsed_date = datetime.strptime(date, '%d-%m-%Y %H:%M:%S')

    result.append(parsed_date.strftime('%Y-%m-%d %H:%M:%S'))

print(result) # Output: ['2023-07-29 12:06:20', '2023-07-28 00:03:05']&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code snippet above reads through the outputdates list, recognizes the two formats, and standardizes them into the 'YYYY-MM-DD hh:mm:ss' format, ignoring any null values.&lt;/P&gt;&lt;P&gt;Remember to adjust the code according to your specific environment or programming language if you are not using Python.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 07:27:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-correct-format-inside-a-multivalue-field/m-p/653528#M225843</guid>
      <dc:creator>Miasm1</dc:creator>
      <dc:date>2023-08-08T07:27:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to create correct format inside a multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-correct-format-inside-a-multivalue-field/m-p/654692#M226177</link>
      <description>&lt;P&gt;Thank you all for the great help!&lt;BR /&gt;All your examples helped me understand some mechanics more in detail.&lt;BR /&gt;&lt;BR /&gt;The solutions from &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/6367"&gt;@bowesmana&lt;/a&gt; was on the point the think i though about but i did not know how to do it.&lt;BR /&gt;&lt;BR /&gt;Kind regards to all of you!&lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2023 13:58:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-correct-format-inside-a-multivalue-field/m-p/654692#M226177</guid>
      <dc:creator>Flenwy</dc:creator>
      <dc:date>2023-08-17T13:58:00Z</dc:date>
    </item>
  </channel>
</rss>

