<?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 Addtotals / Convert String To Number in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116951#M31119</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;in my event the field &lt;STRONG&gt;Amount&lt;/STRONG&gt; can appear several times. The value is an amount of products. Sometimes Splunk identifies field &lt;STRONG&gt;Amount&lt;/STRONG&gt; as a string, but it should always be a number.&lt;/P&gt;

&lt;P&gt;In the end, I want to create a new field that shows the total &lt;STRONG&gt;Amount&lt;/STRONG&gt; for each event:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| addtotals fieldname=totalamount Amount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've already tried out to convert the values:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| convert num(Amount)

| convert auto(Amount)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But this didn't solve the problem.&lt;/P&gt;

&lt;P&gt;Best &lt;/P&gt;

&lt;P&gt;Heinz&lt;/P&gt;</description>
    <pubDate>Thu, 16 Jan 2014 16:30:09 GMT</pubDate>
    <dc:creator>HeinzWaescher</dc:creator>
    <dc:date>2014-01-16T16:30:09Z</dc:date>
    <item>
      <title>Addtotals / Convert String To Number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116951#M31119</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;in my event the field &lt;STRONG&gt;Amount&lt;/STRONG&gt; can appear several times. The value is an amount of products. Sometimes Splunk identifies field &lt;STRONG&gt;Amount&lt;/STRONG&gt; as a string, but it should always be a number.&lt;/P&gt;

&lt;P&gt;In the end, I want to create a new field that shows the total &lt;STRONG&gt;Amount&lt;/STRONG&gt; for each event:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| addtotals fieldname=totalamount Amount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've already tried out to convert the values:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| convert num(Amount)

| convert auto(Amount)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But this didn't solve the problem.&lt;/P&gt;

&lt;P&gt;Best &lt;/P&gt;

&lt;P&gt;Heinz&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2014 16:30:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116951#M31119</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2014-01-16T16:30:09Z</dc:date>
    </item>
    <item>
      <title>Re: Addtotals / Convert String To Number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116952#M31120</link>
      <description>&lt;P&gt;You could convert them all to string using convert tostring() , then convert num()&lt;/P&gt;

&lt;P&gt;Perhaps this post can assist:&lt;BR /&gt;
&lt;A href="http://answers.splunk.com/answers/103101/convert-a-string-into-a-number"&gt;http://answers.splunk.com/answers/103101/convert-a-string-into-a-number&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2014 16:33:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116952#M31120</guid>
      <dc:creator>aelliott</dc:creator>
      <dc:date>2014-01-16T16:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: Addtotals / Convert String To Number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116953#M31121</link>
      <description>&lt;P&gt;Hey,&lt;/P&gt;

&lt;P&gt;Thanks for your answer.&lt;/P&gt;

&lt;P&gt;Unfortunately the solutions from the linked post don't help and converting to a string before doesn't work. The field  n is empty. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval n=tostring(Amount,"commas")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Jan 2014 16:45:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116953#M31121</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2014-01-16T16:45:40Z</dc:date>
    </item>
    <item>
      <title>Re: Addtotals / Convert String To Number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116954#M31122</link>
      <description>&lt;P&gt;I don't think there are added spaces. This doesn't help:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval n=trim(Amount)
| eval m=tostring(n, "commas")
| convert num(m) AS z
| addtotals fieldname=sum z
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Jan 2014 16:58:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116954#M31122</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2014-01-16T16:58:46Z</dc:date>
    </item>
    <item>
      <title>Re: Addtotals / Convert String To Number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116955#M31123</link>
      <description>&lt;P&gt;But eventstats will calculate to total amount over all events and write this value to each event, right?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2014 17:04:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116955#M31123</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2014-01-16T17:04:19Z</dc:date>
    </item>
    <item>
      <title>Re: Addtotals / Convert String To Number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116956#M31124</link>
      <description>&lt;P&gt;Not if you tell it not to. You can split the stats as you want.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2014 17:20:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116956#M31124</guid>
      <dc:creator>Ayn</dc:creator>
      <dc:date>2014-01-16T17:20:29Z</dc:date>
    </item>
    <item>
      <title>Re: Addtotals / Convert String To Number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116957#M31125</link>
      <description>&lt;P&gt;But I would need a unique identifier for each event to do this?!&lt;/P&gt;

&lt;P&gt;And I'm really wondering why eventstats can handle the value types of my "Amount" field and addtotals cannot&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2014 17:22:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116957#M31125</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2014-01-16T17:22:18Z</dc:date>
    </item>
    <item>
      <title>Re: Addtotals / Convert String To Number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116958#M31126</link>
      <description>&lt;P&gt;First - are all the Amount fields spelled the same - no spelling or capitalization variations?&lt;/P&gt;

&lt;P&gt;Second - if Splunk sees this as a multi-valued field, it is a single field with multiple values and &lt;CODE&gt;addtotals&lt;/CODE&gt; can't do that. So you could do this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;yoursearchhere
| eval id=splunk_server . index .  _cd
| mvexpand Amount
| eval amount=tonumber(Amount)
| stats sum(amount) by id
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Every event &lt;EM&gt;can&lt;/EM&gt; be uniquely identified, so I used that in my solution. Of course, after you run this, you just get the amounts and id numbers, which isn't very useful. But you can change the stats command to include additional fields.&lt;/P&gt;

&lt;P&gt;And if you really want to go crazy with it:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;yoursearchhere
| eval id=splunk_server . index .  _cd
| join id [ search yoursearchhere |  eval id=splunk_server . index .  _cd
    | mvexpand Amount
    | eval amount=tonumber(Amount)
    | stats sum(amount) as TotalAmount by id ]
| fields - id
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;At the end of this search, you will have the same events that you started with - but with one additional field called TotalAmount. It will cost you a subsearch and a join, though.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2014 19:12:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116958#M31126</guid>
      <dc:creator>lguinn2</dc:creator>
      <dc:date>2014-01-16T19:12:46Z</dc:date>
    </item>
    <item>
      <title>Re: Addtotals / Convert String To Number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116959#M31127</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;thanks for your input.&lt;BR /&gt;
All fields are spelled the same. Just using &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats sum(Amount) 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;to get the TotalAmount is no problem (your first solution). But I need it as field in the event to calculate more stats later.&lt;/P&gt;

&lt;P&gt;Wouldn't it be easier to use something like this, after creating the eventid:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;my search
| eval id=splunk_server . index .  _cd
| eventstats sum(Amount) AS TotalAmount by id
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;As far as I understand it, this will create a TotalAmount field per event, which can be used in further stats commands.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2014 08:54:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Addtotals-Convert-String-To-Number/m-p/116959#M31127</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2014-01-17T08:54:36Z</dc:date>
    </item>
  </channel>
</rss>

