[Python] Python - Query in framing dynamic sql query

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have this below working query framed to run for given multiple dynamic apps for the Snowflake connection

    sql_query = '''
    select * from TABLE1
    where ({}) and OPEN_TIME > current_date - 10 and OPEN_TIME < current_date - 2
    '''.format(' OR '.join(["lower(appname) like '%{}%'".format(app) for app in app_selection_code]))


    Now I want to make the value 10 also be input variable to run for given period as below

    period =10
    app_selection_code=['Office','Winword']

    sql_query = '''
    select * from TABLE1
    where ({}) and OPEN_TIME > current_date - {period} and OPEN_TIME < current_date - 2
    '''.format(' OR '.join(["lower(appname) like '%{}%'".format(app) for app in app_selection_code]))




    So I converted the above string into f-string (sql_query = f''' select...) but it's giving issue error at the App part when I convert to f-string. Please suggest a solution for this problem

    Continue reading...

Compartilhe esta Página