Getting Data In

Is there a way in which we can filter the records fetched, based on a datetime column by specifying the start and end datetimes?

ursarun
New Member

I have a requirement where i have to filter the records fetched between 2 date times. How to include this filter criteria in the splunk query?

Tags (2)
0 Karma

somesoni2
Revered Legend

Try using subsearch to set the earliest and latest for the Splunk search. Syntax of subsearch would be like this.
Syntax:

[| gentimes start=-1 | eval earliest=strptime("YourStartDateInHumanReadableFormat","FormatOfYourDate") | eval latest=strptime"YourEndDateInHumanReadableFormat","FormatOfYourDate") | table earliest,latest | format]

Usage:

your base search  .. [| gentimes start=-1 | eval earliest=strptime("YourStartDateInHumanReadableFormat","FormatOfYourDate") | eval latest=strptime"YourEndDateInHumanReadableFormat","FormatOfYourDate") | table earliest,latest | format]...| rest of the search

If using in dashboard, you can use token in place of "YourStartDateInHumanReadableFormat" and "YourEndDateInHumanReadableFormat".

0 Karma

aweitzman
Motivator

You'll need to convert the datetime field to an epochtime filed using convert's mktime() function:
http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Convert

Do the same for your beginning and ending datetimes, and then filter using the comparison:

...your search...
| convert timeformat="%Y-%m-%d %H:%M:%S" mktime(fieldtime) as fieldepoch
| eval begintime="2014-01-01 00:00:00"
| eval endtime="2014-12-31 23:59:59"
| convert timeformat="%Y-%m-%d %H:%M:%S" mktime(begintime) as beginepoch
| convert timeformat="%Y-%m-%d %H:%M:%S" mktime(endtime) as endepoch
| where fieldepoch > beginepoch AND fieldepoch < endepoch
0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...