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

Below is the original code from oracle DB
select
decode(acc_number,
'', acc_number,
'123'||acc_number) as acc_number
from table1
i want to convert it on redshift and it is not working
ERROR: invalid input syntax for type numeric:
i try to cast '123' into numeric
select
decode(acc_number,
'', acc_number,
'123'::numeric||acc_number) as acc_number
from table1
but it still not working
update on using case when on redshift
select
case when acc_number = '' then acc_number
else '123'||acc_number
end
from table1
same error
ERROR: invalid input syntax for type numeric:

Related

DB2 Assign result of with clause to variable

I am using DB2 LUW and want to a assign a result of a With clause to a variable in a stored procedure.
I got the exception
{0:0} An unexpected token "AS" was found following "l = (WITH BASE". Expected tokens may include: "JOIN".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.28.11
Is it possible to assign the result on this way or should I have to solve it with a cursor?
DECLARE result CLOB(8M);
SET result = (WITH BASE AS (
xxx
)
SELECT JSON_ARRAY (select json_objects FROM ITEMS format json) FROM SYSIBM.SYSDUMMY1);
Use instead the syntax style:
with ctename AS ( ... ) SELECT ... INTO ... FROM ctename;

Database-migration ms sql to postgresql

I am getting a syntax error when converting between SQL Server to PostgreSQL. Any thoughts?
IF (var_port_with_bmrk_total_mv != 0 AND var_bmrk_info IS NOT NULL) THEN
BEGIN
insert into t$tmp_diff
select #asof_dt asof_dt,#choiceID choiceID ,p.input_array_type ,p.group_order, CONVERT(DECIMAL(32,10),p.port_value/#var_port_total_mv) port_value,convert(decimal(32,10), isnull(bmrk_value/#port_with_bmrk_total_mv,0)) bmrk_value
from t$tmp_port_sum p, t$tmp_bmrk_sum b
where p.input_array_type=b.input_array_type and p.group_order = b.group_order
END;
ELSE
Original before conversion
insert into #tmp_other_diff
select #asof_dt asof_dt,#choiceID choiceID , b.input_array_type,b.grouping,convert(decimal(32,10),0) port_value, (bmrk_value/#port_with_bmrk_total_mv) bmrk_value
from #tmp_bmrk_other_sum b
where b.key_value not in ( select p.key_value from #tmp_port_other_sum p)
Error message:
Error occurred during SQL query execution
Reason:
SQL Error [42601]: ERROR: syntax error at or near ","
Position: 9030
the relevant comma being:
CONVERT(DECIMAL(32,10),p.port_value
There is no convert() function in Postgres. Use the SQL standard cast or the Postgres extension ::data type. In this case:
...., cast(0 as decimal(30,10)) port_value, ....
OR
...., 0::decimal(30,10) port_value, ...
Note: No comma after the expression. In the original port_value is the column alias. You need to keep it that way.

Why do I have to do a type cast in the ELSE part of a PostgreSQL update statement using a CASE, but not in the WHEN part?

I have to update a newly created metadata table (which holds a 1-1 relations with a person table) in PostgreSQL (>=12):
UPDATE public.metadata m
SET print_status =
CASE
WHEN p.print IS NOT NULL THEN 'done'
ELSE 'not done'
END
FROM public.people p
WHERE m.fk_people_id = p.id;
but I face this error:
ERROR: column "print_status" is of type print_statuses but expression is of type text
LINE 18: CASE
^
HINT: You will need to rewrite or cast the expression.
SQL state: 42804
Character: 477
print_statuses is an enum containing ('done', 'doing', 'not done'). And the print_status column of the metadata table is of type print_statuses (it also defaults to 'not done', so I wouldn't need the ELSE part anymore I guess).
It seems I do have to type cast the 'not done' text in the ELSE part of the CASE statement like this for this query to work:
UPDATE public.metadata m
SET print_status =
CASE
WHEN p.print IS NOT NULL THEN 'done'
ELSE 'not done'::print_statuses
END
FROM public.people p
WHERE m.fk_people_id = p.id;
Why do I have to do a type cast here, and not when using a simple UPDATE statement as in:
UPDATE public.metadata m SET print_status = 'not done' FROM public.people p WHERE g.fk_people_id = i.id;
?

ERROR : The column index is out of range: 1, number of columns: 0

I'm using wso2dss 3.0.0.I'm trying to execute a postgresql query i.e.
SELECT addressid, geocode
FROM maddress
WHERE geocode::point <# circle '((18.9750,72.8258), 5)';
It is working fine in PostgreSQL.When i'm using same query in wso2dss i.e.
SELECT addressid, geocode
FROM maddress
WHERE geocode::point <# circle '((?,?), ?)';
It gives me error like :
DS Fault Message: Error in 'SQLQuery.processNormalQuery'
DS Code: DATABASE_ERROR
Source Data Service:-
Name: Geofence_DataService
Location: /Geofence_DataService.dbs
Description: N/A
Default Namespace: http://ws.wso2.org/dataservice
Current Request Name: adding_geofence_op
Current Params: {longitude=72.8258, radius=4, latitude=18.9750}
Nested Exception:-
DS Fault Message: Error in 'createProcessedPreparedStatement'
DS Code: UNKNOWN_ERROR
Nested Exception:-
org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.
if i remove " ' "(quotation mark) of circle then also it will not execute. query '' look like this :
SELECT addressid, geocode
FROM maddress
WHERE geocode::point <# circle ((?,?), ?);
it'll give following error :
Caused by: javax.xml.stream.XMLStreamException: DS Fault Message: Error in 'SQLQuery.processNormalQuery'
DS Code: DATABASE_ERROR
Source Data Service:-
Name: Geofence_DataService
Location: /Geofence_DataService.dbs
Description: N/A
Default Namespace: http://ws.wso2.org/dataservice
Current Request Name: geofence_op
Current Params: {longitude=72.8258, radius=4, latitude=18.9750}
Nested Exception:-
org.postgresql.util.PSQLException: ERROR: function circle(record, double precision) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type cast
But circle is inbuilt geographical function of PostgreSQL then is it necessary to write explicit function? else where is the exact error? Even if i put the query with input parameter as i execute in PostgreSQL then also it's working.If so then why it is not accepting dynamic parameters? Please let me know..
Geometric types can be input in multiple ways.
In the first form, your ? parameters are not replaced with values because they are literal parts of a string. So 0 parameters are expected ...
In the second form without single quotes, your ? parameters are replaced, but ((18.9750,72.8258), 5) is interpreted to be a row type, which doesn't work with circle().
You are trying to invoke the geometric function circle() that takes a point and a double precision ("center and radius to circle"). These are valid syntax variants:
SELECT circle '((18.9750,72.8258), 5)' AS cast_literal
' <(18.9750,72.82580),5>'::circle AS cast_literal2
, circle(point '(18.9750,72.8258)', '5') AS literal_point_n_radius
, circle(point(18.9750,72.8258), '5') AS point_n_literal_radius
, circle(point(18.9750,72.8258), 5) AS point_n_radius
SQL fiddle.
The cast to ::text is just to sanitize the deranged display in SQL fiddle
In your case, to provide numeric values (not a string literal), use the last form and it should work:
SELECT addressid, geocode
FROM maddress
WHERE geocode::point <# circle(point(?,?), ?);
If wso2dss (which I have no experience with) does not accept functions, you have to use one of the first two forms and provide a single parameter as string literal:
SELECT addressid, geocode
FROM maddress
WHERE geocode::point <# circle ?;
... where the parameter is the concatenated literal as displayed above.
You could let Postgres do the concatenation and still pass three numeric values:
SELECT addressid, geocode
FROM maddress
WHERE geocode::point <# ('(('::text || ? || ',' || ? || '),' || ? || ')')::circle;

SQL Command failed with: ERROR: operator does not exist: character varying?

i am trying to get values from database by using following query
SELECT Distinct org.name AS org, sto.ad_org_id AS wh_nearstoredetails_id, sum(sto.qtyonhand) AS qty, pro.name AS product
FROM ad_org org, m_storage_detail sto, m_product pro
WHERE sto.ad_org_id::text = org.ad_org_id::text
AND (sto.m_locator_id::text IN ( select cast(m_locator.m_locator_id as text)
from m_locator,m_warehouse
where m_warehouse.isactive = cast('Y' as varchar)
and m_warehouse.em_ai_warehouseparent::text not like cast('' as text)
and m_warehouse.m_warehouse_id::text = m_locator.m_warehouse_id::text
and m_locator.isdefault = cast('Y' as varchar)))
AND sto.m_product_id::text = pro.m_product_id::text
AND sto.qtyonhand >= cast(1 as numeric)
AND sto.ad_org_id::text IN ( SELECT cast(m_warehouse.ad_org_id as text)
FROM m_warehouse
WHERE m_warehouse.em_ai_warehouseparent::text not like cast('' as text))
GROUP BY org.name,sto.ad_org_id,pro.name
ORDER BY org.name, pro.name;
after creating this i also created a class to call this query and get data but when i deploy my project i am getting following error
WARN - SQL Command failed with: ERROR: operator does not exist: character varying !
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
please help to solve this issue.
i finally found the mistake what i made.
the problem in the above query is i'm trying to make type cast of null value
now i changed the condition to check 'is not null' than "not like ''"
and even i don't need to add cast operation it's useless.
anyways thanks for you response. :)