简体   繁体   中英

Query jsonb fields in the postgres SQL

I'm trying fetch the maximum date from the jsonb column.

I'm getting this below error..

------------------------------------
id  |       obj_value(jsonb) 
------------------------------------
1   | {"value": [{"date": "2020-12-2020"}]} 
------------------------------------
2   | {"value": [{"date": "2020-12-2019"}]} 
---------------------------------
3   | {"value": [{"date": "2020-12-2018"}]} 
---------------------------------

here is the SQL query

SELECT max(obj_value->'value'->0->'effectiveDate')
FROM public.tbl_data  
where obj_value->'value'->0->>'effectiveDate' <= TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD')

I'm getting this below error..

ERROR:  function max(jsonb) does not exist
LINE 1: SELECT max(obj_value->'value'->0->'effectiveDate')
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 8

-> operator returns JSONB type,
->> retuns TEXT type.
You should use ->> and then cast to a type you need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM