I am trying to write a SQL statement in Python: 'attribute' is a column name that I want to change its format and I am giving it as a parameter. Because its name can be different.
cur.execute("SELECT DATEADD(y," + attribute + ", '1980-01-01')")
But I am getting below error. attribute=Date1 and this column exists.
[42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'Date1'. (207) (SQLExecDirectW)"
If I understand your code correctly, you're building the string in the cur.execute
command. If your python is up-to-date, try using fstrings . They are a bit more readable and you don't get the messy code with all the quotes. If your python version doesn't support substrings, try building the request in a variable to make the code a bit more readable.
Possible solution:
cur.execute(f"SELECT DATEADD(y, {attribute}, '1980-01-01')")
The string will result in SELECT DATEADD(y, Date1, '1980-01-01')
There is also a FROM
missing from the query, the error Invalid column
is correct because you don't tell where to find that column
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.