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
Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...