I need to create a sql using PgPreparedStatement(postgres) query which has like concat operator. The query looks similar to this:
Select * from guest gg where
gg.city LIKE CONCAT('PHOENI_' , '%');
This looks pretty easy but the sql query is created using some Custom classes and so when
pstmt.setString(1, x); // dummy value 1
then the value of x here is includes ‘%’ and the query is not formed properly. Below are the variations I have used in ‘x’ and corresponding query formed:
What I see is that if the target string does not have any single quotes , then PreparedStatement would add single quotes around the target String(case 1 of the image), but if I add single quotes anywhere inside the target string, then PreparedStatement would add single quote before every single quote encountered.
I have tried the solution presented here: Using “like” wildcard in prepared statement but still the same result.
The issue I think is that the string value that PreparedStatement would use to replace the marker will always have the “%” (thats how the classes are designed. these are very old and cannot be atleast for changed now). Basically, I cannot do something like this:
Select * from guest gg where gg.city LIKE CONCAT(? , '%');
pstmt.setString(1, "xyz"); // the string that will be used to replace the marker will either have "%" or not.
So, with these limitations, is there a way to have the query as
Select * from guest gg where
gg.city LIKE CONCAT('PHOENI_' , '%');
Thanks in advance!
1