PostgreSQL in list, mixing string and int - postgresql

We are using PostgreSQL 11 and have a query from Redmine database. It is a query that works fine in MySQL 8 but on PostgreSQL we get an error.
SELECT fixed_version_id
FROM issues WHERE
((issues.fixed_version_id IN ('current_version','2')));
ERROR: invalid input syntax for integer: "current_version"
LINE 1: ...d FROM issues WHERE ((issues.fixed_version_id IN ('current_v...
I understand that fixed_version_id is an int and that I quering strings. However, is other SQL like MySQL 8 you can do this and it actually returns values. But in PostgreSQL we get an error. Not sure if we have it setup wrong or if this is just the way PostgreSQL works?
Any help would be most appreciated thank you.
We ran this query
SELECT fixed_version_id
FROM issues
WHERE ((issues.fixed_version_id IN ('current_version','2')));
We were expecting Not to get an error.

SQL is a tightly typed language (seems MySql does not adhere to the standard). The only correction is using the correct type - in this case integer. But you can `CAST' an integer to text and compare.
SQL Standard:
WHERE ((cast (issues.fixed_version_id as text) IN ('current_version','2')));
Postgresql extension:
WHERE ((issues.fixed_version_id::text IN ('current_version','2')));

Related

MSSQL to POSTGRES conversion

Anyone can help me with the following query from mssql to postgres?
DateAdd(Week,-(doc.[DelaiLivraisonNum]),Convert(date,sas.DateFin)) as IdDate
where DelaiLivraisonNum and DateFin are columns
Thanks
I always struggle with the SQL Server syntax, but I think this would be the equivalent (it's hard to tell as you did not explain what your code does):
datefin::date - make_interval(weeks => delailivraisonnum)
This assumes that datefin can be cast to a date (e.g. because it's a timestamp) and delailivraisonnum is an integer.

Postgres bytea error when binding null to prepared statements

I am working with a Java application which uses JPA and a Postgres database, and I am trying to create a flexible prepared statement which can handle a variable number of input parameters. An example query would best explain this:
SELECT *
FROM my_table
WHERE
(string_col = :param1 OR :param1 IS NULL) AND
(double_col = :param2 OR :param2 IS NULL);
The idea behind this "trick" is that if a user specifies only one parameter, say :param1, we can just bind null to :param2, and the WHERE clause would then behave as if only the first parameter were even being checked. This approach lets us handle, in theory, any number of input parameters using a single prepared statement, instead of needing to maintain many different statements.
I have gotten a simple POC working locally using pure JDBC prepared statements. However, doing so required casting the parameter before comparing it to NULL, e.g.
WHERE (double_col = ? OR ?::numeric IS NULL)
^^ does not work without cast
However, my actual application is using JPA, and I keep getting the following persistent error:
Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: double precision = bytea
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
The problem does not occur with string/text columns, but only with columns which are double precision in my Postgres table. I have tried all combinations of casting, and nothing works:
(double_col = :param2 OR CAST(:param2 AS double precision) IS NULL);
(CAST(double_col AS double precision) = :param2 OR :param2 IS NULL);
(CAST(double_col AS double precision) = :param2 OR CAST(:param2 AS double precision) IS NULL);
The error seems to be saying that JDBC is sending Postgres a bytea type for the double columns, and then Postgres is rolling over because it can't find a way to cast byte to double precision.
The Java code looks something like:
Query query = entityManager.createNativeQuery(sqlString, MyEntity.class);
query.setParameter("param1", "some value");
// bind other parameters here
List<MyEntity> = query.getResultList();
For reference, here are the versions of everything I am using:
Hibernate version | 4.3.7.Final
Spring data JPA vesion | 1.7.1.RELEASE
Postgres driver version | 42.2.2
Postgres database version | 9.6.10
Java version | 1.8.0_171
Not having received any feedback in the form of answers or even a comment, I was getting ready to give up, when I stumbled onto this excellent blog post:
How to bind custom Hibernate parameter types to JPA queries
The post gives two options for controlling the types which JPA passes through the driver to Postgres (or whatever the underlying database actually is). I went with the approach using TypedParameterValue. Here is what my code looks like continuing with the example given above:
Query query = entityManager.createNativeQuery(sqlString, MyEntity.class);
query.setParameter("param1", new TypedParameterValue(StringType.INSTANCE, null));
query.setParameter("param2", new TypedParameterValue(DoubleType.INSTANCE, null));
List<MyEntity> = query.getResultList();
Of course, it is trivial to be passing null for every parameter in the query, but I am doing this mainly to show the syntax for the text and double columns. In practice, we would expect at least a few of the parameters to be non null, but the above syntax handles all values, null or otherwise.
If you want to keep using plain queries with automatic parameter binding, you could try the following.
WHERE (? IS NULL OR (CAST(CAST(? AS TEXT) AS DOUBLE PRECISION) = double_col
This seems to satisfy the PostgreSQL driver's type checks as well as yielding the correct results. I haven't done much testing, but the performance hit seems minimal because the CASTs happen on a constant value rather than rows from the database.

How to Fix Postgres Numeric Auto Become 6 Decimal Places When Using Insert Into OPENQUERY from SQL Server

I found an issue of Postgres decimal places auto become 6 places when try to insert data from SQL Server into Postgres using OPENQUERY.
I have searched many references that suggested using CAST or Convert to limit decimal places from SQL Server, everything is work fine when I just tried select from the SQL Server side (It is 0.001), but whenever run the query like below, in Postgres (for example the 'Rounding' will become 0.001000).
For example:
INSERT INTO OPENQUERY(RND,
'SELECT
name,
rounding
FROM test.public.product_uom')
SELECT
UoMID,
0.001
FROM dbo.tUoM
WHERE UoMID IN ('YEAR', 'ZAK');
The expected result that I would like is to have the same value of Rounding when Insert into from SQL Server to Postgres that is 0.001. Any help or suggestions will be appreciated and thanks in advance.

Laravel Eloqeunt CAST() table column with postgres generating error

I am using Laravel 5.2 with Postgres. I have an integer column where I need to put a condition with like %%. I have a postal_code columns where integers values are saved. Now I need to get all rows those have 12 in that. But we cannot do that with integer datatype so I am casting it as text while doing query but I am getting error.
Here is my code
$query->where("cast(postal_code as TEXT)", "LIKE", "%".$request['postal_code']. "%");
And it is generating error
"cast(postal_code" as "text)"
Please see the unwanted " being put in query. This is the query output error. Is this the error Or there is some thing other went wrong and how can I fix that.
I think casting a field on where in laravel doesn't seem to be worked.Execute a raw query using whereRaw function.
$query->WhereRaw("cast(postal_code as TEXT) ILIKE '%?%',[$request['postal_code']]");

ERROR In Sequences query if postgres

I create one sequence in postgres and fire one query which is mentioned below
SELECT M_PRODUCTSEQ.NEXTVAL from DUAL;
but it gives me the below error:
ERROR: relation "dual" does not exist.
Kindly help me out. How can i made the relation with dual?
PostgreSQL does NOT support the from DUAL syntax. It does however make the from portion of a query like this optional, so getting the next value (nextval) of a sequence you would do something like this:
SELECT nextval('m_productseq');