Redshift - ERROR: Invalid input syntax for type numeric - amazon-redshift

I use the aws - redshift now,but i got a Error like below when i run this query
<Query>
select
row_number() over(partition by nb.roadnumbercode
order by sqrt(power((1.0*cast(ra.latitude as decimal(38,10)))-coalesce(nb.buildingcenterlatitude,0),2)
+power((1.0*cast(ra.longitude as decimal(38,10)))-coalesce(nb.buildingcenterlongitude,0),2)
)) rnum
from dmart.addresses ra join dmart.buildings nb on nb.roadnumbercode = ra.roadnamecode and nb.buildingcenterlatitude is not null and nb.buildingcenterlongitude is not null
limit 30;
Why this error cause ? How can I fix it ?

It looks like you are casting the empty string (“”) to a numeric which isn’t valid. You need to handle the case where the string is empty, nut just NULL.

Related

SQL Error [22P02]: ERROR: invalid input value for enum tableName.date_unit: ""

I got this error when I try to get the list of all rows on my table. This only happens because I include one of the column in the SELECT. The column itself is an enum column and I wanna use COALESCE for the column in case it meets a null value.
This is a simplication of my code
SELECT id,
user_id,
coalesce(date_unit, '') date_unit
FROM table_name
WHERE user_id = $1
I got this error when I try to run it
SQL Error [22P02]: ERROR: invalid input value for enum table_name.date_unit: ""
This is the error when I run it using SQLX On Golang
Pq: invalid input value for enum table_name.date_unit: \"\"
date_unit itself is an enum which has restricted values. It only accepts day and month as value in the table. But lots of rows have null value in date_unit.
I wanna convert it to "" or empty string if date_unit value is null.
Is there a problem with the COALESCE with enum values? How should I use COALESCE to work with what I wanna do?
The answer is found in the comment section of the question.
To officiate it, as date_unit is not a string type, it cannot be returned when querying (invalid data type). As such, when querying, we should convert date_unit to string type.
This can be done using the query:
SELECT id,
user_id,
COALESCE(date_unit::text, '')
FROM table_name
WHERE user_id = $1
SELECT id,
user_id,
coalesce(date_unit, '')
FROM table_name
WHERE user_id = $1

Update with ISNULL and operation

original query looks like this :
UPDATE reponse_question_finale t1, reponse_question_finale t2 SET
t1.nb_question_repondu = (9-(ISNULL(t1.valeur_question_4)+ISNULL(t1.valeur_question_6)+ISNULL(t1.valeur_question_7)+ISNULL(t1.valeur_question_9))) WHERE t1.APPLICATION = t2.APPLICATION;
I know you cannot update 2 tables in a single query so i tried this :
UPDATE reponse_question_finale t1
SET nb_question_repondu = (9-(COALESCE(t1.valeur_question_4,'')::int+COALESCE(t1.valeur_question_6,'')::int+COALESCE(t1.valeur_question_7)::int+COALESCE(t1.valeur_question_9,'')::int))
WHERE t1.APPLICATION = t1.APPLICATION;
But this query gaves me an error : invalid input syntax for integer: ""
I saw that the Postgres equivalent to MySQL is COALESCE() so i think i'm on the good way here.
I also know you cannot add varchar to varchar so i tried to cast it to integer to do that. I'm not sure if i casted it correctly with parenthesis at the good place and regarding to error maybe i cannot cast to int with coalesce.
Last thing, i can certainly do a co-related sub-select to update my two tables but i'm a little lost at this point.
The output must be an integer matching the number of questions answered to a backup survey.
Any thoughts?
Thanks.
coalesce() returns the first non-null value from the list supplied. So, if the column value is null the expression COALESCE(t1.valeur_question_4,'') returns an empty string and that's why you get the error.
But it seems you want something completely different: you want check if the column is null (or empty) and then subtract a value if it is to count the number of non-null columns.
To return 1 if a value is not null or 0 if it isn't you can use:
(nullif(valeur_question_4, '') is null)::int
nullif returns null if the first value equals the second. The IS NULL condition returns a boolean (something that MySQL doesn't have) and that can be cast to an integer (where false will be cast to 0 and true to 1)
So the whole expression should be:
nb_question_repondu = 9 - (
(nullif(t1.valeur_question_4,'') is null)::int
+ (nullif(t1.valeur_question_6,'') is null)::int
+ (nullif(t1.valeur_question_7,'') is null)::int
+ (nullif(t1.valeur_question_9,'') is null)::int
)
Another option is to unpivot the columns and do a select on them in a sub-select:
update reponse_question_finale
set nb_question_repondu = (select count(*)
from (
values
(valeur_question_4),
(valeur_question_6),
(valeur_question_7),
(valeur_question_9)
) as t(q)
where nullif(trim(q),'') is not null);
Adding more columns to be considered is quite easy then, as you just need to add a single line to the values() clause

JPA Named query gives Error as The right expression is not a valid expression and The query contains a malformed ending

I have the Named query as below and passing dynamic parameter
select count(table1) from Table1 table1 where table1.nameId in
(select table2.nameId from Table2 table2 where table2.hicNumber =:value0)
and table1.indicator = :value1
and table1.startDate = :value2
and NVL(table1.endDate,'31-DEC-2999')=NVL(:value3,'31-DEC-2999')
It gives Error as
[235, 255] The right expression is not a valid expression.
[256, 297] The query contains a malformed ending.
Can you please help me for what i am doing wrong?

Passing a null parameter to a native query using #Query JPA annotation

In a Spring Boot application, I have a SQL query that is executed on a postgresql server as follows :
#Query(value = "select count(*) from servers where brand= coalesce(?1, brand) " +
"and flavour= coalesce(?2, flavour) ; ",
nativeQuery = true)
Integer icecreamStockCount(String country, String category);
However,
I get the following error when I execute the method :
ERROR: COALESCE types bytea and character varying in PostgreSQL
How do I pass String value = null to the query?
**NOTE : ** I found that my question varied from JPA Query to handle NULL parameter value
You need not coalesce, try this
#Query("select count(*) from servers where (brand = ?1 or ?1 is null)" +
" and (flavour = ?2 or ?2 is null)")
Integer icecreamStockCount(String country, String category);
When I encounted this error, I ended up using a combination of OR and CAST to solve the issue.
SELECT COUNT(*)
FROM servers
WHERE (?1 IS NULL OR brand = CAST(?1 AS CHARACTER VARYING))
AND (?2 IS NULL OR flavour = CAST(?2 AS CHARACTER VARYING))
This works even if ?1, ?2, brand and flavor are all nullable fields.
Note that passing null for ?1 means "all servers regardless of brand" rather than "all servers without a brand". For the latter, you could use IS DISTINCT FROM as follows.
SELECT COUNT(*)
FROM servers
WHERE (CAST(?1 AS CHARACTER VARYING) IS NOT DISTINCT FROM brand)
AND (CAST(?2 AS CHARACTER VARYING) IS NOT DISTINCT FROM flavour)
Finally, certain parameter types such as Boolean cannot be cast in SQL from BYTEA to BOOLEAN, for those cases you need a double cast:
SELECT COUNT(*)
FROM servers
WHERE (?1 IS NULL OR is_black = CAST(CAST(?1 AS CHARACTER VARYING) AS BOOLEAN))
In my eyes this is a problem in Hibernate which could be solved by passing Java null parameters as plain SQL NULLs rather than interpreting null as a value of type BYTEA.
If you really need to use native query, there is a problem because it's an improvement not implemented yet, see hibernate. If you don't need to use native you can do (where ?1 is null or field like ?1). Assuming you do need native,
you may treat the String before by setting this empty and then calling the repository and this one would be like:
#Query(value = "select count(*) from servers where (?1 like '' or brand like ?1) " +
"and (?2 like '' or flavour like ?2)",
nativeQuery = true)
Integer icecreamStockCount(String country, String category);
There is always javax.persistence.EntityManager bean as option for native query situations and I recommend it instead of previous approach. Here you can append to your query the way you want, as follows:
String queryString = "select count(*) from servers ";
if (!isNull(country)) queryString += "where brand like :country";
Query query = entityManager.createNativeQuery(queryString);
if (!isNull(country)) query.setParameter("country", country);
return query.getResultList();
Observations:
Newer versions have improved this '+' concatenation Strings. But you can build your queryString the way you want with StringBuilder or String Format, it doesn't matter.
Be careful with SQL injection, the setParameter method avoid this kind of problem, for more information see this Sql Injection Baeldung
So this is not the exact answer to the question above, but I was facing a similar issue, I figured I would add it here, for those that come across this question.
I was using a native query, in my case, it was not a singular value like above, but I was passing in a list to match this part of the query:
WHERE (cm.first_name in (:firstNames) OR :firstNames is NULL)
I was getting the bytea error, in the end I was able to send an empty list.
(null == entity.getFirstName()? Collections.emptyList() : entity.getFirstName())
In this case, sending the empty list to the resolver worked, where as null did not.
hope this saves you some time.
null parameters are not allowed before Hibernate 5.0.2.
See https://hibernate.atlassian.net/browse/HHH-9165
and the replies to https://www.postgresql.org/message-id/6ekbd7dm4d6su5b9i4hsf92ibv4j76n51f#4ax.com

DateAdd and date fields not working as a criteria for a query

The query I am working on has worked before. I don't know the exact date since I inherited it when my co-worker left. Every time I run the query I get a "Data type Mismatch in criteria expression."
SELECT Personnel.Badge, Personnel.First_Name, Personnel.Last_Name, Training.Course, DateAdd("m",[Cert_Duration],[Latest_Course_Date]) AS Cert_Expiration, Personnel.Reports_To
FROM Training INNER JOIN (Personnel INNER JOIN qry_Training_Log_Latest_Course ON Personnel.Badge = qry_Training_Log_Latest_Course.Personnel) ON Training.Training_ID = qry_Training_Log_Latest_Course.Course
WHERE (((DateAdd("m",[Cert_Duration],[Latest_Course_Date]))<DateAdd("m",6,Date())) AND ((Training.Cert_Duration) Is Not Null));
It appears the line
I would appreciate any feedback on this.
This query also triggers "Data type Mismatch in criteria expression."
SELECT DateAdd("m", Null, Date());
Therefore your error is likely caused by Null [Cert_Duration] values in this expression ...
DateAdd("m",[Cert_Duration],[Latest_Course_Date])
You need to revise the query to either ...
Exclude rows with Null [Cert_Duration] values before the DateAdd() is evaluated.
Or substitute something else for Null [Cert_Duration] values in that expression.