Re: [GENERAL] How to define the limit length for numeric type? - Mailing list pgsql-general

From Tom Lane
Subject Re: [GENERAL] How to define the limit length for numeric type?
Date
Msg-id 15358.1489336741@sss.pgh.pa.us
Whole thread Raw
In response to Re: [GENERAL] How to define the limit length for numeric type?  ("David G. Johnston" <david.g.johnston@gmail.com>)
List pgsql-general
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Sun, Mar 12, 2017 at 12:00 AM, vod vos <vodvos@zoho.com> wrote:
>> The INSERT action still can be done. What I want is just how to limit the
>> length of the insert value, you can just type format like 59.22, only four
>> digits length.

> length(trunc(goose, 0)::text) + scale(goose)
> I suspect you might encounter some issues, namely around
> 123.456789::numeric(6,1) casting behavior and maybe
> 00059.12000::numeric(6,1) treatment of unimportant zeros.

Yeah.  I wonder if the OP wouldn't be better off thinking of his data as
strings rather than numbers.  The format requirement could be expressed
as a CHECK constraint, along the lines of length(goose) = 5 AND
goose ~ '^\d+\.\d+$' (or possibly \d* if zero digits on one side of
the decimal point is OK).  You could imagine storing as numeric and
having CHECK constraints that cast to string and make those tests,
but I fear trailing zeroes would break it.

            regards, tom lane


pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: [GENERAL] How to define the limit length for numeric type?
Next
From:
Date:
Subject: [GENERAL] general erros backup