Sorry, of course, the correct SQL throwing the error is:
select '"1.453"'::jsonb::real;
On Tue, May 31, 2022 at 6:08 PM Wells Oliver <wells.oliver@xxxxxxxxx> wrote:
Well, it's because the representation of the value is getting double quoted, so it'sselect '"1.453"'::real, which yields ERROR: cannot cast jsonb string to type real.I have all this serialized string data stored in the jsonb col, all of which is double quoted.On Tue, May 31, 2022 at 5:50 PM Tom Lane <tgl@xxxxxxxxxxxxx> wrote:Wells Oliver <wells.oliver@xxxxxxxxx> writes:
> When I know this value is a floating point value I would just like to cast
> SELECT value::real but you can't do that. You can do SELECT
> (jsonbcol->>'somekey')::real, but these columns just store a value.
Works for me, in v11 and up:
regression=# select '1.4531'::jsonb::real;
float4
--------
1.4531
(1 row)
In older versions you could do
regression=# select '1.4531'::jsonb::text::real;
float4
--------
1.4531
(1 row)
I fear that either one involves a conversion to text and back
under the hood :-(. Now, if you were casting to numeric,
that would be very cheap.
regards, tom lane
--Wells Oliver
wells.oliver@xxxxxxxxx
Wells Oliver
wells.oliver@xxxxxxxxx
wells.oliver@xxxxxxxxx