Running two different queries based on client time of day

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

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:

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

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

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
1 Like

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