Aug-08-2024, 10:59 AM
Hello everyone,
In an application I wrote, I need to change the date information entered by the user in the format DD/MM/YYYY to YYYY/MM/DD in order to write it to the SQL database field.
I am using the following code structure for this change, and I get the correct output as YYYY/MM/DD.
However, when it comes to writing this data to SQL, I receive the following error message.
I am using 'date' or 'datetime' as the data type in the database, but it doesn't make a difference.
Does anyone have any ideas on this issue?
ERROR MESSAGES:
Error: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Error converting data type nvarchar to numeric. (8114) (SQLExecDirectW)')
CODE:
In an application I wrote, I need to change the date information entered by the user in the format DD/MM/YYYY to YYYY/MM/DD in order to write it to the SQL database field.
I am using the following code structure for this change, and I get the correct output as YYYY/MM/DD.
However, when it comes to writing this data to SQL, I receive the following error message.
I am using 'date' or 'datetime' as the data type in the database, but it doesn't make a difference.
Does anyone have any ideas on this issue?
ERROR MESSAGES:
Error: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Error converting data type nvarchar to numeric. (8114) (SQLExecDirectW)')
CODE:
from datetime import datetime, date date = request.form['DATE'] print(date) date_str = datetime.strptime(date, '%d/%m/%Y') date_sql_format = datetime.strptime(date_str.strftime('%Y-%m-%d'), '%Y-%m-%d').date() print(date_sql_format) conn = pyodbc.connect(connection_string) cursor = conn.cursor() cursor.execute('''INSERT INTO dbo.Users (DATE) VALUES (?) ''', (date_sql_format)) conn.commit() cursor.close() conn.close() date = 05/08/2024 date_sql_format = 2024-08-05