将 varchar 值 '06-03-21' 转换为数据类型 int 时转换失败

Conversion failed when converting the varchar value '06-03-21' to data type int

我正在尝试将我的Python脚本中的字符串变量与varchar变量进行比较在 SQL 中。以下是我的代码片段:

todaysDate = datetime.now().date().strftime('%d-%m-%y')

read(conn, f'select cou_id from NewCourier where date_created = {todaysDate}'

其中:

todaysDate => python 中的变量存储当前日期

cou_id & date_created => 关系数据库中的列 table NewCourier

我试过了:

read(conn, f'select cou_id from NewCourier where CAST(date_created AS INT) = {todaysDate}

我还在 Whosebug 和其他网站上浏览了几个问题和解决方案,只找到 this 与我的问题类似的解决方案,但我不清楚如何解决它。

谢谢!

Python 3.8.5

微软 SQL 服务器管理工​​作室 18

列数据类型似乎是整数,您正试图将其与字符串进行比较。如果您可以在程序中找到插入 table 的代码,您应该能够弄清楚如何正确转换日期时间

使用 SQL 的 GETDATE() 方法 并转换了它,而不是在 Python 中创建一个变量来存储当前日期使用 SQL 中的 CONVERT() 方法 到 varchar 解决了我的问题

(即,它允许我获取当天生成的所有记录的快递 ID)。

这是更新后的行:

read(conn, f'select cou_id from NewCourier where date_created = CONVERT(varchar, getdate(), 23)