How to add/subtract an integer to an array of integers? - postgresql

select
array[100,200,300]-100
My expectation was to get [0,100,200].
I got this error:
ERROR: operator does not exist: integer[] - integer Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Position: 26
How can I fix it?

You can use a combination of array functions: unnest and array_agg:
postgres=# SELECT array_agg(val-100) as arr FROM unnest(array[100,200,300]) as val;
arr
-------------
{0,100,200}
(1 row)

Related

How can I convert a jsonb nested string field to int in postgresql?

I am using postgresql 11 and I have a jsonb field.
amount
----------------------------------------
{"value": "3590", "currency": "AUD"}
I can use this syntax to select the nested field value: select amount->'value'.
but I don't know how I can convert it to integer and sum it, like select sum(amount->'value') from
I got this error: HINT: No function matches the given name and argument types. You might need to add explicit type casts.
I tried to convert it by
select sum(amount->'value')::DECIMAL
select sum(TO_NUMBER(amount->'value'))
but none of them working. What is the correct way to do that?
Use the ->> operator to get the value as text:
https://www.postgresql.org/docs/11/functions-json.html
SELECT SUM((amount->>'value')::INT)

ERROR: operator does not exist: numeric = character varying

This is my query where i am getting below error..
SELECT incident_id, incident_name, incident_desc, incident_status,
root_cause_level_1,
root_cause_level_2, remediate_proj_name, remediate_proj_desc, remediate_proj_phase, start_date, end_date
FROM incident_details
INNER JOIN complain_details ON complain_details.complaint_id = incident_details.incident_id;
ERROR: operator does not exist: numeric = character varying
LINE 3: ...complain_details ON complain_details.complaint_id = incident...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
I have try to type cast not work.

ERROR 42883: operator does not exist: text / text

When running the following query:
select data from example
where (data -> 'properties' ->> 'outageCount') / (data -> 'properties' ->> 'trackedCount') > 0.01
I'm getting the following error:
ERROR 42883: operator does not exist: text / text
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Both outageCount and trackedCount are stored as integers in the JSONB.
I tried casting using as float but it gave following error:
[42601] ERROR: syntax error at or near "as"
Even if the field is a JSON number, it will be returned as text (see json and jsonb operators):
db=# select pg_typeof(('{"foo":3}'::jsonb)->>'foo');
pg_typeof
-----------
text
(1 row)
You need to cast it by appending ::float to the expression:
db=# select pg_typeof((('{"foo":3}'::jsonb)->>'foo')::float);
pg_typeof
------------------
double precision
(1 row)
In your case:
select data from example
where (data -> 'properties' ->> 'outageCount')::float / (data -> 'properties' ->> 'trackedCount')::float > 0.01

syntax used to type cast date(data type_ to integer in postgresql

I want convert Date data type to Integer, i have tried to type cast the date(data type) to integer by using ::INT, its not working.
I have got the following error
ERROR: column "date" is of type integer but expression is of type
date HINT: You will need to rewrite or cast the expression.
What you can do is:
replace(your_date::varchar, '/', '')::integer
Full example:
select replace(now()::date::varchar, '-', '')::integer
Use the TO_CHAR function with FM prefix and then cast it to Integer.
select to_char(date_column,'FMddFMmmYYYY')::INT as dateint FROM t
Demo

Postgres: ERROR: operator does not exist: character varying = bigint

My query is something like this. I try to get a status for a list of ids.
select order_number, order_status_name
from data.order_fact s
join data.order_status_dim l
on s.order_status_key = l.order_status_key
where
order_number in (1512011196169,1512011760019,1512011898493,1512011972111)
I get an error though that says:
ERROR: operator does not exist: character varying = bigint
LINE 6: order_number in (1512011196169,1512011760019,1512011898493,1...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Do you have any clue on how I should reform the ids to get it work?
Thanks a lot!
Your order_number is a varchar, you can't compare that to a number (123 is a number in SQL, '123' is a string constant)
You need to use string literals:
order_number in ('1512011196169','1512011760019','1512011898493','1512011972111')
More details in the manual:
http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS
If you can't change the type of numbers within in, you could use cast:
select * from numbers_as_string
where cast(my_numbers_as_string as int) in (1,2,3)
This happen too when you are using native query in sprinboot and you are passing a parameter as string but that field is a (integer or long) in your model/entity, also when you are comparing a string with a integer due that param is used like string without casting.
so you should cast it as integer in the native query like this
x\:\:integer
for example:
#Query(value="
......
.....
inner join tablex t on t.x\\:\\:integer = pv.id \n"+
....
....
")
List<Object> getMyQuery(#Param("x") String x)