Re: Suggested (or existing) way to parse currency into numeric?

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Suggested (or existing) way to parse currency into numeric?
Date: 2015-06-12 17:11:32
Message-ID: CAKFQuwboG6eaNrX5ouzOo=sF=50uf+mCuJ_QVVL8vzQeMhU5FA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Jun 12, 2015 at 12:57 PM, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
wrote:

> On 06/12/2015 09:46 AM, David G. Johnston wrote:
>
>> Version 9.3
>> CREATE TABLE t ( field numeric NULL );
>> SELECT * FROM json_populate_record(null::t, '{ "field": "$18,665"
>> }'::json);
>> Error: invalid input syntax for type numeric: "$18,665"
>>
>> I can accept the type of field being something like "numeric_cleaned"
>> which has a custom input function that would strip away the symbols and
>> commas (not too concerned about locale at the moment...) and am
>> pondering writing my own custom type with supporting SQL function to
>> accomplish that but I'm hoping the community can point me to something
>> already existing.
>>
>> I really want to avoid going through a staging table. I'm more inclined
>> to brute force the source JSON using "jq" (or sed) before I would go
>> that route.
>>
>> Thoughts, suggestions, comments?
>>
>
> test=> CREATE TABLE t ( field money NULL );
> CREATE TABLE
> test=> SELECT * FROM json_populate_record(null::t, '{ "field": "$18,665"
> }'::json);
> field
> ------------
> $18,665.00
> (1 row)
>

​I wrote that type off as something I would never code into my own schema
so basically forgot about its usability in other situations.

Thank you for the reminder.

David J.​

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2015-06-12 17:16:38 Re: Suggested (or existing) way to parse currency into numeric?
Previous Message Adrian Klaver 2015-06-12 16:57:28 Re: Suggested (or existing) way to parse currency into numeric?