簡體   English   中英

如何執行sql查詢包括python的時間間隔條件

[英]How to execute sql query include where condition of time interval by python

我想通過 pycharm IDE 使用 pyodbc 庫執行 SQL 查詢。我的數據源是 Microsoft SQL 服務器。

import pandas as pd SQL_Query = pd.read_sql_query('select * from tblprofilewhimp_norm where MeterpointID = 111602 and Date_M > 2016-11-01 00:15:00', conn)

我收到此錯誤:

pandas.io.sql.DatabaseError: Execution failed on sql 'select * from tblprofilewhimp_norm where MeterpointID = 111602 and Date_M > 2016-11-01 00:15:00': ('42000', "[42000] [Microsoft][ODBC Driver 17對於 SQL Server][SQL Server]'00' 附近的語法不正確。(102) (SQLExecDirectW)"

但是,它不起作用,因為 SQL 服務器中的日期時間的形成可能遵循這個 '2016-11-01 00:15:00'。 另一方面,SQL 服務器日期時間格式無法滿足 python 格式。

因此,我該如何糾正這個問題?

您可以像這樣使用 f-string (f" ") 功能:

SQL_Query = pd.read_sql_query(f"select * from tblprofilewhimp_norm where MeterpointID = 111602 and Date_M > '2016-11-01 00:15:00'", conn)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM