-
Notifications
You must be signed in to change notification settings - Fork 565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Odd behavior with MS SQL and datetimes -- INSERT, SELECT values match but WHERE values don't match #1399
Comments
Do you get the expected output for the last query if you use this? print(cursor.execute("SELECT date FROM datetime_test WHERE date = ?", str(t2)[:23]).fetchall()) |
Yes. I do. That returns the expected row. Should I be converting all The actual code I'm working on gets a bunch of rows then updates select rows in specific ways. I was getting a rows = cursor.execute("SELECT ... WHERE ...").fetchall()
for row in rows:
new_value = modify(row.column_one)
cursor.execute("UPDATE ... SET value = ? WHERE ... AND datetime = ?", new_value, row.datetime) |
This appears to be a limitation when working with SQL Server's With a print(cursor.execute("SELECT date FROM datetime_test WHERE date = ?", t2).fetchall()) results in this being executed on the server
As you noted, this works okay for SELECT date FROM datetime_test WHERE date = '2024-05-06 07:08:09.123000' fails with
whereas this one succeeds and returns the correct row SELECT date FROM datetime_test WHERE date = '2024-05-06 07:08:09.123' which is why the |
That's odd. Do you know why the INSERT works fine with that datetime? The INSERT line shows it successfully converting that very same datetime object to a value that gets inserted. If the column precision is 3, should pyodbc be truncating the string to 3 digits of milliseconds before passing the string on to the server? |
SQL Server does appear to be inconsistent, and there may be some low-level reason for it on the server side, but I don't know why.
That would be nice, but it would also require pyodbc to essentially |
Environment
Issue
datetime
objects with 3 digits of milliseconds can be inserted into the DB. When selected, the values coming out match the values going in, so the conversion in and out seems to work. However, when using the same value for a WHERE clause, the values do not match.Excepted: using the inserted datetime object in a WHERE clause should find the row.
Actual: some datetime objects which were inserted/selected cannot be used to select said row.
Test code:
Actual output:
Expected output:
Is this expected behavior or is there a bug here?
The text was updated successfully, but these errors were encountered: