Splunk Dev

How can i filter date?

mrccasi
Explorer

Hi all. I would like to ask on how can I filter a date with a format "Feb-16" "Month-Year" in search command.

Report Month

May-15
Dec-15
Jan-16
Feb-16
Mar-16
Apr-16

And the result should be Feb-16 onward.

Report Month

Feb-16
Mar-16
Apr-16

Thank you.

Tags (1)
0 Karma
1 Solution

DalJeanis
Legend

The strptime function unfortunately does not include a default to the first day of the month when extracting Mmm-yy or YYYY-MM format dates. Therefore, you have to concatenate a 01 onto the end and tell splunk that the 01 is the day.

This makes your test data...

| makeresults | eval mydate="May-15 Dec-15 Jan-16 Feb-16 Mar-16 Apr-16" | makemv mydate | mvexpand mydate

This converts it to epoch time and a display version of the epoch time...

| eval mydateEpoch=strptime(mydate."-01","%b-%y-%d")
| eval mydateDisplay=strftime(mydateEpoch,"%Y-%m-%d")

Either one of these filters for dates from Feb 2016 forward...

| where mydateDisplay>="2016-02-01"

...or...

| where mydateEpoch >= strptime("2016-02-01","%Y-%m-%d")

For efficiency reasons, you always want to filter out records as early as possible, before doing calculations, so this would be the proper order...

| makeresults | eval mydate="May-15 Dec-15 Jan-16 Feb-16 Mar-16 Apr-16" | makemv mydate | mvexpand mydate

| eval mydateEpoch=strptime(mydate."-01","%b-%y-%d")
| where mydateEpoch >= strptime("2016-02-01","%Y-%m-%d")
| eval mydateDisplay=strftime(mydateEpoch,"%Y-%m-%d")

View solution in original post

0 Karma

DalJeanis
Legend

The strptime function unfortunately does not include a default to the first day of the month when extracting Mmm-yy or YYYY-MM format dates. Therefore, you have to concatenate a 01 onto the end and tell splunk that the 01 is the day.

This makes your test data...

| makeresults | eval mydate="May-15 Dec-15 Jan-16 Feb-16 Mar-16 Apr-16" | makemv mydate | mvexpand mydate

This converts it to epoch time and a display version of the epoch time...

| eval mydateEpoch=strptime(mydate."-01","%b-%y-%d")
| eval mydateDisplay=strftime(mydateEpoch,"%Y-%m-%d")

Either one of these filters for dates from Feb 2016 forward...

| where mydateDisplay>="2016-02-01"

...or...

| where mydateEpoch >= strptime("2016-02-01","%Y-%m-%d")

For efficiency reasons, you always want to filter out records as early as possible, before doing calculations, so this would be the proper order...

| makeresults | eval mydate="May-15 Dec-15 Jan-16 Feb-16 Mar-16 Apr-16" | makemv mydate | mvexpand mydate

| eval mydateEpoch=strptime(mydate."-01","%b-%y-%d")
| where mydateEpoch >= strptime("2016-02-01","%Y-%m-%d")
| eval mydateDisplay=strftime(mydateEpoch,"%Y-%m-%d")
0 Karma

gfreitas
Builder

Is this "Report Month" text or Splunk _time? If it`s Splunk time you can use the time picker, you can also eval a new field based on _time with the epoch month and try to use a where month_epoch > epoch_base_month
Another way can be eval a new field with year + month in number and filter like 1602 (for Feb 16), 1603 (for Mar 16), 1604 (for Apr 16)

0 Karma

adonio
Ultra Champion
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...