# Running two different queries based on client time of day

**URL:** <https://forum.inductiveautomation.com/t/running-two-different-queries-based-on-client-time-of-day/17430>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [March 20, 2018, 12:49am UTC](https://forum.inductiveautomation.com/t/running-two-different-queries-based-on-client-time-of-day/17430 "2018-03-20T00:49:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cagibson](https://avatars.discourse-cdn.com/v4/letter/c/e480ec/32.png) [@cagibson](https://forum.inductiveautomation.com/u/cagibson)\
**Post date:** [March 20, 2018, 12:49am UTC](https://forum.inductiveautomation.com/t/running-two-different-queries-based-on-client-time-of-day/17430/1 "2018-03-20T00:49:27Z")

</div>

There are two SQL scrips I have and they are to be ran depending on what time of day (e.i. 18:00 to 23:59, and 00:00 to 18:00). I have a Table object that can only take one SQL Query for the Data property binding. I’m trying to write an expression for the Property Binding: Root Container.Table as follows:

import datetime

now = datetime.datetime.now()

if 18 \< now.hour \<= 23: project.main.SQL\_query\_1():

where the SQL\_query\_1() has been defined as a function on the main script.

Thanks for the help!!!

---

<div class="post-metadata">

**Author:** ![bfuson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bfuson/32/4597_2.png) [@bfuson](https://forum.inductiveautomation.com/u/bfuson)\
**Post date:** [March 20, 2018, 6:36pm UTC](https://forum.inductiveautomation.com/t/running-two-different-queries-based-on-client-time-of-day/17430/2 "2018-03-20T18:36:09Z")

</div>

What exactly is your issue here? Are you _sure_ you need to use 2 different queries, and you can’t simply update the query parameters depending on the time?

If you’re sure you need to do it this way, then I’d recommend bundling both query options in one project script. That will let you simplify your expression binding on your data table to:

```auto
runScript("project.main.runShiftQuery", 5000)

```

And then in the project.main script file, define your functions as:

```auto
def runShiftQuery():
    hour = system.date.getHour24(system.date.now())
    query1stShift = """
                    SELECT * FROM a_table WHERE conditions
                    """
    query2ndShift = """
                    SELECT * FROM a_table WHERE conditions
                    """
    result = None
    if 18 < hour <= 23:
        result = system.db.runQuery(query1stShift, "yourDB")
    else:
        result = system.db.runQuery(query2ndShift, "yourDB")
    return result

```

---

<div class="post-metadata">

**Author:** ![cagibson](https://avatars.discourse-cdn.com/v4/letter/c/e480ec/32.png) [@cagibson](https://forum.inductiveautomation.com/u/cagibson)\
**Post date:** [March 20, 2018, 7:28pm UTC](https://forum.inductiveautomation.com/t/running-two-different-queries-based-on-client-time-of-day/17430/3 "2018-03-20T19:28:31Z")

</div>

this is great! What I ended up doing was writing to a boolean tag \_true \_ or _false_ depending on what the time of day it is. Then I had two tables with the two queries in each. one table would be visible only when the boolean tag was true, and viceversa for the other table.

and yes, i’m pretty certain I couldn’t accomplish the query in one SQL script. the issue stemmed from the day change over at midnight when using now() in SQL, etc.

I’m going to try your function though, it is much cleaner.

Thank you bfuson
