0

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
1
  • 1
    Use ->> instead of ->, just like you did in the WHERE condition Commented Jan 24, 2021 at 14:29

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.