查询以比较带日期的日期和不带日期的日期-使用Access DB的python [英] Query to compare between date with time and date without time - python using access db
问题描述
我需要帮助来创建查询,以比较带日期的日期和带时间的日期.我正在将Python与Access db(pypyodbc)配合使用.
I need help to create query to compare between date with time and date without time. I am using python with access db (pypyodbc).
在数据库中,我有一个包含日期/时间(包括时间)的列,而在python中,我有一个datetime对象(没有时间).
In the database I have a column that contains date/time (includes time), and in python I have a datetime object (without time).
我想编写一个仅比较两个日期的sql查询.
I want to write a sql query that compares just the dates of the two.
例如:
cur.execute("SELECT * FROM MDSSDB WHERE [ValidStartTime] = #2016-05-17#")
ValidStartTime包含时间,因此它不起作用.我只需要ValidStartTime中的日期.
The ValidStartTime includes time so it doesn't work. I want just the date from the ValidStartTime.
推荐答案
Consider using MS Access' DateValue
function that extracts only the date component (TimeValue
being the time component counterpart).
此外,考虑将日期值作为参数传递,以更好地与Python环境集成,而无需连接到Access的#
形式.下面将参数作为一项的元组传递:
Also, consider passing your date value as parameter to better integrate with your Python environment with no need to concatenate into Access' #
form. Below passes a parameter as tuple of one item:
from datetime import datetime
...
cur.execute("SELECT * FROM MDSSDB WHERE DateValue([ValidStartTime]) = ?", (datetime(2016, 5, 17),))
这篇关于查询以比较带日期的日期和不带日期的日期-使用Access DB的python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!