pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Wells Oliver <wells.oliver@gmail.com>
Cc: pgsql-admin <pgsql-admin@postgresql.org>
Subject: Re: Casting json (or jsonb) to real
Date: Tue, 31 May 2022 20:50:51 -0400
Message-ID: <378261.1654044651@sss.pgh.pa.us> (raw)
In-Reply-To: <CAOC+FBW85OU9bRFC0ozNtiWFcGkMjCLv3fuVH8LSbiZ8qcX8_g@mail.gmail.com>
References: <CAOC+FBW85OU9bRFC0ozNtiWFcGkMjCLv3fuVH8LSbiZ8qcX8_g@mail.gmail.com>

Wells Oliver <wells.oliver@gmail.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





view thread (6+ messages)  latest in thread

Message-ID: <378261.1654044651@sss.pgh.pa.us>
Permalink:  ../378261.1654044651@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/378261.1654044651@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: tgl@sss.pgh.pa.us, wells.oliver@gmail.com
  Subject: Re: Casting json (or jsonb) to real
  In-Reply-To: <378261.1654044651@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox