Postgresql pass function parameter with casting - postgresql

I created two functions. The first function, getProductOrder, is getting token whose type is character varying. The second function is getOrderTechnicalDetails whose function parameter is character varying.
For example When I call getProductOrder with quotation marks that is working nothing problem the function
Worked example
select
(select row_to_json(getOrderTechnicalDetails('991964b80d4f41a416b48ac99bdc07ad')) as techDetails),
o.*
from
(select * from getProductOrder(null, '54e91016-46d0-11e7-912d-001dd8b72237', null, 9181, 1::smallint)) o
But I have to use dynamic parameter here problem is casting problem function can't determine casting.
When I execute this query I get syntax error. How can I solve this problem?
Query
select
(select row_to_json(getOrderTechnicalDetails(o.token::character varying)) as techDetails),
o.*
from
(select * from getProductOrder(null, '54e6341-46d0-11e7-912d-001dd8b72237', null, 9181, 1::smallint)) o
Error
ERROR: invalid input syntax for integer: "-"
SQL state: 22P02
Context: SQL function "getordertechnicaldetails" statement 1

Related

PostgreSQL execute SELECT * FROM (t as a); return ERROR: syntax error at or near ")"

Why these SQLs can't work in PostgreSQL?
SELECT * FROM (t as a);
or
SELECT * FROM (t);
ERROR: syntax error at or near ")"
Those SQLs work well in MySQL.
Well, it's invalid SQL.
If you want to give the table an alias you need to use from t as a.
The form from (....) requires a valid query inside the parentheses and t as a (or just t) is not a valid query. Additionally: a derived table (which is what (...) defines) requires an alias. So at least it has to be from (...) as bla
To get all rows and all columns from a table the SQL standard provides the shortcut table some_table which is supported by Postgres.
So the following would be valid:
select * from (table t) as a
Typically, table alias' are applied where there is a join. For example:
SELECT alias1.login_name, alias2.name
FROM tablename1 AS alias1
JOIN tablename2 AS alias2
ON alias1.id = alias2.role_id;
To apply an alias to a single table:
SELECT * FROM tablename AS alias;
..will do it for you, no need for parentheses.
You can confirm/test by example below if value is an integer:
SELECT * FROM tablename AS alias
WHERE alias.colum = value;
..or if value is a string:
SELECT * FROM tablename AS alias
WHERE alias.colum = 'value';
postgreSQL is strongly-typed, so the first example above will work for Int and Bool values, however if you have Date or other values, you may need to apply type casting. See this link for helpful info*: www.postgresqltutorial.com/postgresql-tutorial/postgresql-cast.
**Remember: For psql strings, always use single quotes for values, use double quotes for table names and column names.

PostgreSQL: Conditional where clause using regex not working

I am trying to implement conditional where clause that sets id when maybe_numeric is really numeric, but the following does end up with ERROR: invalid input syntax for integer: "xxx" for non-numeric values.
How can I fix the first condition so that the second condition is evaluated only if the maybe_numeric is numeric?
SELECT *
FROM some_table st
WHERE NOT ${maybe_numeric} ~ '^\d+$' OR st.id = ${maybe_numeric}::int

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?

Use function result as columns in query with PostgreSQL

I'm using PostgreSQL 9.1 and let's say I have a type in PostgreSQL like:
CREATE TYPE sticker AS (
customer_id integer,
customer_machine_id integer,
);
and I have a plpgsql named get_sticker that returns the type sticker...
I can do this fine:
select get_sticker(a_value), * from foo_bar;
But, this returns the result in a tuple (which totally makes sense). But, how can I convert (basically unpack) to columns?
It seems like it would be something like the following, but it fails.
select get_sticker(a_value).*, * from foo_bar; -- <<<< FAIL
Error message:
ERROR: syntax error at or near "."
SQL state: 42601
You need an extra set of parentheses:
select (get_sticker(a_value)).*, * from foo_bar;

Postgres query error

I have a query in postgres
insert into c_d (select * from cd where ak = '22019763');
And I get the following error
ERROR: column "region" is of type integer but expression is of type character varying
HINT: You will need to rewrite or cast the expression.
An INSERT INTO table1 SELECT * FROM table2 depends entirely on order of the columns, which is part of the table definition. It will line each column of table1 up with the column of table2 with the same order value, regardless of names.
The problem you have here is whatever column from cd with the same order value as c_d of the table "region" has an incompatible type, and an implicit typecast is not available to clear the confusion.
INSERT INTO SELECT * statements are stylistically bad form unless the two tables are defined, and will forever be defined, exactly the same way. All it takes is for a single extra column to get added to cd, and you'll start getting errors about extraneous extra columns.
If it is at all possible, what I would suggest is explicitly calling out the columns within the SELECT statement. You can call a function to change type within each of the column references (or you could define a new type cast to do this implicitly -- see CREATE CAST), and you can use AS to set the column label to match that of your target column.
If you can't do this for some reason, indicate that in your question.
Check out the PostgreSQL insert documentation. The syntax is:
INSERT INTO table [ ( column [, ...] ) ]
{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) | query }
which here would look something like:
INSERT INTO c_d (column1, column2...) select * from cd where ak = '22019763'
This is the syntax you want to use when inserting values from one table to another where the column types and order are not exactly the same.