Re: Casting json (or jsonb) to real

Поиск
Список
Период
Сортировка
От Wells Oliver
Тема Re: Casting json (or jsonb) to real
Дата
Msg-id CAOC+FBVgADFvgpwTeSB=vU=Lv2ea_HvJZdZSZr5A7B=9d_p0Ww@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Casting json (or jsonb) to real  (Tom Lane <tgl@sss.pgh.pa.us>)
Ответы Re: Casting json (or jsonb) to real  (Wells Oliver <wells.oliver@gmail.com>)
Re: Casting json (or jsonb) to real  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-admin
Well, it's because the representation of the value is getting double quoted, so it's 

select '"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@sss.pgh.pa.us> wrote:
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


--

В списке pgsql-admin по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Casting json (or jsonb) to real
Следующее
От: Wells Oliver
Дата:
Сообщение: Re: Casting json (or jsonb) to real