Splunk Search

DBConnect, $earliest$ and $latest$

devicenul1
Path Finder

Anyway to pass the earliest and latest variables from a time range picker to the DB Connect Query command in a specified format?

for example:

|dbquery LiveDB "select * from table between '$earliest$="%Y-%m-%d"' and '$latest$="%Y-%m-%d"'"

Because DB Query needs to be the first command of a search I've tried doing the eval's as a subsearch, something like:

|dbquery LiveDB "select * from table between '$early$' and '$late$' [eval e=$earliest$ | eval l=$latest$ | eval early=strptime(e, "%Y-%m-%d") | eval late=strptime(l, "%Y-%m-%d") ]

but doing that seems to just put it into a "Search is waiting for input" state ... any ideas?

1 Solution

devicenul1
Path Finder

The $earliest$ and $latest$ variables are in epoch time, so I just wrote a stored procedure that converts it to DATE and runs the query based on that.

SQL:

Create Procedure [dbo].[procedureName]
    @EpochBeginDate INT,
    @EpochEndDate INT
AS
BEGIN
Set NOCOUNT ON

DECLARE @BeginDate DATE;
DECLARE @EndDate DATE;
IF @EpochBeginDate IS NULL
BEGIN
    --Defaulting to three months ago
    SELECT @BeginDate = DATEADD(MONTH, -3, GETDATE()); 
    END
ELSE 
BEGIN
    --adding EpochBeginDate to 01-01-1970
    SELECT @BeginDate = DATEADD(s, @EpochBeginDate, '19700101'); 
    END

IF @EpochEndDate IS NULL
BEGIN
    --Defaulting to Today
    SELECT @EndDate = GETDATE(); 
    END
ELSE 
BEGIN
    --adding EpochEndDate to 01-01-1970
    SELECT @EndDate = DATEADD(s, @EpochEndDate, '19700101'); 
    END


-- Insert statements for procedure here
SELECT
FROM
WHERE
END

View solution in original post

0 Karma

devicenul1
Path Finder

The $earliest$ and $latest$ variables are in epoch time, so I just wrote a stored procedure that converts it to DATE and runs the query based on that.

SQL:

Create Procedure [dbo].[procedureName]
    @EpochBeginDate INT,
    @EpochEndDate INT
AS
BEGIN
Set NOCOUNT ON

DECLARE @BeginDate DATE;
DECLARE @EndDate DATE;
IF @EpochBeginDate IS NULL
BEGIN
    --Defaulting to three months ago
    SELECT @BeginDate = DATEADD(MONTH, -3, GETDATE()); 
    END
ELSE 
BEGIN
    --adding EpochBeginDate to 01-01-1970
    SELECT @BeginDate = DATEADD(s, @EpochBeginDate, '19700101'); 
    END

IF @EpochEndDate IS NULL
BEGIN
    --Defaulting to Today
    SELECT @EndDate = GETDATE(); 
    END
ELSE 
BEGIN
    --adding EpochEndDate to 01-01-1970
    SELECT @EndDate = DATEADD(s, @EpochEndDate, '19700101'); 
    END


-- Insert statements for procedure here
SELECT
FROM
WHERE
END
0 Karma

gfreitas
Builder

Hi, I have the same problem. The only workaround I could do is to pipe the results and compare the time like this: "| where date_field > relative_time(now(),"-1mon@mon")".

devicenul1
Path Finder

Ahh I see, that's a good workaround but I'd really like to limit my searches out of SQL to only relevant data. This seems like such a simple thing. I've exhausted all research into figuring out how to do this either as an inline search or through the xml dashboards. I've moved my research into using the HTML dashboards and trying to catch this via the javascript. I'll update this thread if I make any progress.

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 ...