Thread: Postgres SQL unable to handle Null values for Text datatype
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 64
> org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = bytea This has been discussed on Stack Overflow[0]. The answer with the highest approval suggests to use coalesce[1]: ``` Select * from A where middle_name = coalesce(?1) ``` Lutz [0] https://stackoverflow.com/a/54223586 [1] https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL
We are migrating from Oracle 12C to Aurora Postgres 13 and running into query failures when the bind value of a Text datatype resolves to nul
Oracle is actually in the wrong here. Nothing should be equal to null, ever. There is also different behavior with unique indexes:
[mgogala@umajor ~]$ docker start psql14-5
psql14-5
[mgogala@umajor ~]$ psql -U scott
Password for user scott:
psql (14.5)
Type "help" for help.
scott=> create table test1 (key1 integer,key2 integer, data varchar(10));
CREATE TABLE
scott=> alter table test1 add constraint test1_uq unique(key1,key2);
ALTER TABLE
scott=> insert into test1 values(1,null,'aaaaa');
INSERT 0 1
scott=> insert into test1 values(1,null,'bbbbb');
INSERT 0 1
scott=> select * from test1;
key1 | key2 | data
------+------+-------
1 | | aaaaa
1 | | bbbbb
(2 rows)
The same thing would not work with Oracle. However, please note that, according to SQL standard, NULL is not equal to anything, to those 2 rows are actually not a unique constraint violation. To enforce the uniqueness the same way as with Oracle, you actually need 2 indexes. You need to use the "coalesce" function.
-- Mladen Gogala Database Consultant Tel: (347) 321-1217 https://dbwhisperer.wordpress.com
> On Sep 5, 2022, at 23:10, Karthik K L V <venkata.karthik4u@gmail.com> wrote: > The above query fails with the below exception when the value of ?1 resolves to null. > org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = bytea > Hint: No operator matches the given name and argument types. You might need to add explicit type casts. > Position: 64 This is a Hibernate issue. If you use setParameter with a NULL without specifying the type, it assumes bytea (with the PostgreSQLJDBC driver, at least). You'll need to use the three-parameter form of setParameter() that specifies a type, ifthe value is going to be NULL. You can also use the setXXXX methods on SQLQuery, since the type is specified by the particularmethod there. That being said, PostgreSQL's handling of NULL string values is different from Oracle's, and this is an area that code changesare often required.