0

I'm trying to get date from excel, but only in those cases if I inserted it manualy, otherwise I want SQL to insert current end of month automatically by the (date_trunc('month', CURRENT_DATE) + interval '1 month' - interval '1 day')::date) expression.

But if I get the date from excel, I have to put 'date_eom' variable in quotes in SQL query builder or I get an error, but if I get date from 'cur_eom' variable I have to remove quotes in SQL query and cant understand how to combine these two clauses.

Code bellow:

sheet = xw.sheets('Inserter')

last_row = sheet.range('A' + str(sheet.cells.last_cell.row)).end('up').row
data = sheet.range(f"A8:H{last_row}").options(ndim=2).value 


cur_eom = "(date_trunc('month', CURRENT_DATE) + interval '1 month' - interval '1 day')::date)"


    for i in data:
        date_eom = f'{i[5].date()}' if i[5] is not None else cur_eom

        with connection.cursor() as cursor:
            cursor.execute(f'''
                insert into lifecashflow_default.cash_flow (date_eom)
                values (
                        {date_eom})
                ''')
  • Use parameter substitution instead of string formatting, as shown in the linked duplicate. It handles the quoting automatically, including edge cases like embedded quotes. – snakecharmerb Jan 21 '22 at 13:51

0 Answers0