Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
831 B
2
Indexable
Never
from datetime import datetime

# Generate an INSERT query dynamically
insert_query = f"INSERT INTO {schema_ext}.{table_name} ("
insert_columns = []
insert_values = []

for column_name, source_val in source_row.items():
    if source_val is not None:
        try:
            # Attempt to parse the value as a datetime
            parsed_datetime = datetime.strptime(source_val, '%Y-%m-%d %H:%M:%S')
            formatted_datetime = f"TO_DATE('{parsed_datetime.strftime('%Y-%m-%d %H:%M:%S')}', 'YYYY-MM-DD HH24:MI:SS')"
            insert_values.append(formatted_datetime)
        except ValueError:
            insert_values.append(f"'{source_val}'")
    else:
        insert_values.append('NULL')

insert_query += ", ".join(insert_columns)
insert_query += ") VALUES ("
insert_query += ", ".join(insert_values)
insert_query += ")"