pyodbc: No result for SQL-select if character field in where-clause does not match in length using parameter (ORACLE)
Hello,
I am using PyODBC 4.0.14 with Python 3.4.4 on Windows 7 64-Bit and connect to a Oracle 11gR2 database running on a SLES12 machine. The ODBC Driver I am using comes with the Client installation of Oracle: 11.02.00.01
When I execute a select-Statement that contains a string parameter (NCHAR in Oracle) I do not get any results if the length of the parameter and the length of the database field do not exactly match.
See this minimal example:
""" create table foo
(
colnum number(20) not null,
colchar1 nchar(6) not null,
colchar2 nchar(20) not null
);
insert into foo values (1, 'foobar', 'foobar');
"""
import pyodbc
conn = pyodbc.connect("...")
curs = conn.cursor()
curs.execute("select * from foo;")
print(curs.fetchall()) # Works
curs.execute("select * from foo where colnum = ? and colchar1 = ?;", [ 1, "foobar"] )
print(curs.fetchall()) # Works
curs.execute("select * from foo where colnum = ? and colchar2 = ?;", [ 1, "foobar"] )
print(curs.fetchall()) # Does not work
curs.execute("select * from foo where colnum = ? and colchar2 = ?;", [ 1, "foobar".ljust(20) ] )
print(curs.fetchall()) # Works
This does only happen for Oracle Databases. I have tested this with Postgres 9.6.2 and the issue does not come up. I have yet to test this on a Linux machine and maybe try Oracle 12c - but I don’t expect changes.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 18 (6 by maintainers)
It seems Oracle does follow the ANSI 92 standard of ignoring trailing spaces when comparing strings (https://docs.oracle.com/cd/A97630_01/appdev.920/a96624/b_char.htm#175), but the issue here may be the result of using parameter values. This StackOverflow answer suggests that the parameterized values are sent with specific typing that force trailing spaces to be included in the comparison.