【问题标题】:Date issue - python to oracle sql日期问题 - python 到 oracle sql
【发布时间】:2022-01-12 01:15:03
【问题描述】:

我有一个 run_dt 在创建表时产生问题,列数据类型会自行更改为 Char。

print(execution_dt)
2020-10-05 14:24:25.352317    

run_dt= execution_dt.date()


print(run_dt)
2020-10-05

run_dt=str(run_dt)
print(run_dt)
2020-10-05

type(run_dt)
<class 'str'>

我有 Oracle db 作为后端和 python 来编写脚本。

cursor.execute(f'''create table taxes
select age,
   name,
   '{run_dt}' as max_upload_date,
    from Employee a left outer join code cd on (s.emp_id = cd.code)
    and s.transaction_date = {repr(run_dt)}
''')

如果我执行上面的 python 代码,它会创建一个表格 tax,但是 max_upload_date - column datatype 会自动变成 - Char。

如果我按以下方式应用 to_date 函数:

to_date('{run_dt}')  as max_upload_date,

我收到以下错误:

错误 - cx_Oracle.DatabaseError: ORA-01861: 文字与格式字符串不匹配

请帮忙。提前致谢。

【问题讨论】:

    标签: python sql python-3.x oracle-sqldeveloper cx-oracle


    【解决方案1】:

    TO_DATE 应该没问题,但前提是您应用了适当的格式掩码(而不是让 Oracle 猜测该做什么;如您所见,它失败了):

    to_date('2020-10-05', 'yyyy-mm-dd')
    

    (如果这是 1005 所代表的;反之亦然。我不知道,但你应该)

    【讨论】:

    • to_date('{run_dt}','YYYY-MM-DD') as max_upload_date, --- 我是如何使用它并且它工作的。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多