Let me explain:
[1] [no error]
SELECT (('{"a": null}'::JSONB)->>'a')::INT;
[2] [error here]
SELECT (t.value::TEXT)::INT FROM jsonb_each(('{"a": null}'::JSONB)) AS t
I have to use jsonb_each function.
How can i make SQL number 2 return null?
Let me explain:
[1] [no error]
SELECT (('{"a": null}'::JSONB)->>'a')::INT;
[2] [error here]
SELECT (t.value::TEXT)::INT FROM jsonb_each(('{"a": null}'::JSONB)) AS t
I have to use jsonb_each function.
How can i make SQL number 2 return null?
In your first example you use ->> which returns the value as a text data type. The equivalent "for each" function is jsonb_each_text() which also returns the value as text. jsonb_each returns each value as a JSONB value
SELECT t.value::INT
FROM jsonb_each_text(('{"a": null}'::JSONB)) AS t
You can do this for example:
SELECT nullif(t.value::text,'null')::int
FROM jsonb_each(('{"a": null}'::JSONB)) AS t
Best regards,
Bjarni