HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

According to the SQL92 spec, can I store the value 1 in a field where the precision = scale?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
canthefieldwhereprecisionstorevaluesql92scaleaccording

Problem

Is it SQL92 compliant to store the number 1 in a field that is defined as NUMERIC 3,3?

What about DECIMAL 3,3?

Does this mean that the precision is a floating decimal place or static that must have 3 decimal places even if they are all zero?

Solution

The precision is the total number of decimal digits that can be stored on both sides of the decimal place. The scale is the total number of decimal digits to the right of the decimal point which can be less than or equal to the precision. Therefore the maximum value that can be stored in a DECIMAL(3,3) or a NUMERIC(3,3) is 0.999. The easiest way to find this out is to actually try it out.

Using mysql 5.5.20 the below returns 0.999:

SET @val = (SELECT CAST(1 AS DECIMAL(3,3)));
SELECT @val;


Whereas the following returns 1:

SET @val = (SELECT CAST(1 AS DECIMAL(3,0)));
SELECT @val;


I hope this helps.

EDIT: I forgot to mention that on most platforms attempting to perform the above will result in some type of Arithmetic Overflow Exception. For instance the below statement running on Microsoft SQL Server 2008 will throw an error:

DECLARE @val DECIMAL(3,3);
SET @val = 1;
SELECT @val;

Code Snippets

SET @val = (SELECT CAST(1 AS DECIMAL(3,3)));
SELECT @val;
SET @val = (SELECT CAST(1 AS DECIMAL(3,0)));
SELECT @val;
DECLARE @val DECIMAL(3,3);
SET @val = 1;
SELECT @val;

Context

StackExchange Database Administrators Q#22027, answer score: 5

Revisions (0)

No revisions yet.