Re: Data Type for Money

Lists: pgsql-general
From: Carlos Mennens <carlos(dot)mennens(at)gmail(dot)com>
To: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Data Type for Money
Date: 2011-12-30 17:41:18
Message-ID: CAAQLLO6QR7VvuirL8Jne_fQOY=rF6rg2P_kZazOwE1AF-Ce_Eg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I'm trying to understand what is the recommended data type for $ in
PostgreSQL. I've done some research and from what I've gathered, there
are a few options:

1. decimal
2. money

I've read the 'money' data type is non-standard and I should avoid
using this. I see it a bunch of Microsoft SQL Server which I assume
works great but I'm using PostgreSQL and want to make sure I'm ANSI
SQL compliant. I would normally just use 'decimal' however when I'm in
doubt, I use pgAdmin3 as a cheat sheet and upon building a new column,
under 'data type', there is no option for decimal but there is for
money. I'm very confused as I assumed 'money' was a non-standard
option for SQL Server and 'decimal' was the correct value but it's not
an option in the pgAdmin3 GUI.

Any tips and or advice?


From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Cc: Carlos Mennens <carlos(dot)mennens(at)gmail(dot)com>
Subject: Re: Data Type for Money
Date: 2011-12-30 17:46:25
Message-ID: 201112300946.25910.adrian.klaver@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Friday, December 30, 2011 9:41:18 am Carlos Mennens wrote:
> I'm trying to understand what is the recommended data type for $ in
> PostgreSQL. I've done some research and from what I've gathered, there
> are a few options:
>
> 1. decimal
> 2. money
>
> I've read the 'money' data type is non-standard and I should avoid
> using this. I see it a bunch of Microsoft SQL Server which I assume
> works great but I'm using PostgreSQL and want to make sure I'm ANSI
> SQL compliant. I would normally just use 'decimal' however when I'm in
> doubt, I use pgAdmin3 as a cheat sheet and upon building a new column,
> under 'data type', there is no option for decimal but there is for
> money. I'm very confused as I assumed 'money' was a non-standard
> option for SQL Server and 'decimal' was the correct value but it's not
> an option in the pgAdmin3 GUI.
>
> Any tips and or advice?

My guess is it is listed as numeric which is equivalent to decimal:

http://www.postgresql.org/docs/9.1/interactive/datatype-numeric.html

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com


From: Carlos Mennens <carlos(dot)mennens(at)gmail(dot)com>
To: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Data Type for Money
Date: 2011-12-30 18:10:13
Message-ID: CAAQLLO63X3xs+1HSyG_dQ9Drr=DQc6Fg0jxTAeCPrC5p_XbSNA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, Dec 30, 2011 at 12:46 PM, Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com> wrote:
> My guess is it is listed as numeric which is equivalent to decimal:
>
> http://www.postgresql.org/docs/9.1/interactive/datatype-numeric.html

Thanks. I just for some reason can't see or understand the difference
between 'decimal' & 'numeric'. Why have two data types for the same
values? Am I missing something?


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Carlos Mennens <carlos(dot)mennens(at)gmail(dot)com>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Data Type for Money
Date: 2011-12-30 18:27:52
Message-ID: 20211.1325269672@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Carlos Mennens <carlos(dot)mennens(at)gmail(dot)com> writes:
> Thanks. I just for some reason can't see or understand the difference
> between 'decimal' & 'numeric'. Why have two data types for the same
> values? Am I missing something?

There isn't any difference, in Postgres. There are two type names
because the SQL standard requires us to accept both names. In a quick
look in the standard it appears that the only difference is this:

17)NUMERIC specifies the data type exact numeric, with the decimal
precision and scale specified by the <precision> and <scale>.

18)DECIMAL specifies the data type exact numeric, with the decimal
scale specified by the <scale> and the implementation-defined
decimal precision equal to or greater than the value of the
specified <precision>.

ie, for DECIMAL the implementation is allowed to allow more digits than
requested to the left of the decimal point. Postgres doesn't exercise
that freedom so there's no difference between these types for us.

regards, tom lane


From: Dann Corbit <DCorbit(at)connx(dot)com>
To: 'Carlos Mennens' <carlos(dot)mennens(at)gmail(dot)com>, PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Data Type for Money
Date: 2011-12-30 18:35:48
Message-ID: 87F42982BF2B434F831FCEF4C45FC33E5037272F@EXCHANGE.corporate.connx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

From:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms710150(v=vs.85).aspx

ODBC type SQL_DECIMAL maps to SQL type DECIMAL(p,s)
Signed, exact, numeric value with a precision of at least p and scale s. (The maximum precision is driver-defined.) (1 <= p <= 15; s <= p).[4]

ODBC type SQL_NUMERIC maps to SQL type NUMERIC(p,s)
Signed, exact, numeric value with a precision p and scale s (1 <= p <= 15; s <= p).[4]

Footnote [4]:
[4] SQL_DECIMAL and SQL_NUMERIC data types differ only in their precision. The precision of a DECIMAL(p,s) is an implementation-defined decimal precision that is no less than p, whereas the precision of a NUMERIC(p,s) is exactly equal to p.

For ODBC, numeric values are stored in a structure of type SQL_NUMERIC_STRUCT:
struct tagSQL_NUMERIC_STRUCT {
SQLCHAR precision;
SQLSCHAR scale;
SQLCHAR sign[g];
SQLCHAR val[SQL_MAX_NUMERIC_LEN];[e], [f]
} SQL_NUMERIC_STRUCT;

If you examine the contents of the ODBC include file sqltypes.h you will see how the values are stored internally for ODBC transmission of the data from SQL to C.

The major difference between the two types is that DECIMAL is of arbitrary precision, defined by the driver vendor, and must contain at least 15 digits of precision in maximum precision columns, but could contain more significant digits up to a driver specified maximum.

So DECIMAL may possibly hold more digits than NUMERIC can. In the case of PostgreSQL, the number of possible significant digits for decimal is immense (1000 digits, IIRC). More than anyone other than a theoretical mathematician would ever need.
I believe in the case of PostgreSQL, when you declare a column of type NUMERIC, you will actually create a DECIMAL column. So for all practical purposes they are synonyms in PostgreSQL. If you bind as an ODBC type, you will live under the limitations of ODBC binding.

-----Original Message-----
From: pgsql-general-owner(at)postgresql(dot)org [mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Carlos Mennens
Sent: Friday, December 30, 2011 10:10 AM
To: PostgreSQL
Subject: Re: [GENERAL] Data Type for Money

On Fri, Dec 30, 2011 at 12:46 PM, Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com> wrote:
> My guess is it is listed as numeric which is equivalent to decimal:
>
> http://www.postgresql.org/docs/9.1/interactive/datatype-numeric.html

Thanks. I just for some reason can't see or understand the difference between 'decimal' & 'numeric'. Why have two data types for the same values? Am I missing something?

--
Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org) To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general