Re: Casting json (or jsonb) to real

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Wells Oliver <wells(dot)oliver(at)gmail(dot)com>
Cc: pgsql-admin <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Casting json (or jsonb) to real
Date: 2022-06-01 00:50:51
Message-ID: 378261.1654044651@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Wells Oliver <wells(dot)oliver(at)gmail(dot)com> 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

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Wells Oliver 2022-06-01 01:08:54 Re: Casting json (or jsonb) to real
Previous Message Wells Oliver 2022-06-01 00:31:26 Casting json (or jsonb) to real