How to Create a Column with a Configurable Floating-Point Type in SQL Server

At first I tried it with a column of type Real, so the column definition was, for example, Column(1,1), but that always gave me an error “Parameter or variable ‘Column’ has an invalid data type.”. So I tried it with the Decimal data type instead. In general, let’s take the column definition Column(2,1) as an example.

The 2 here means the precision/length, i.e. the total number of digits, or in other words the sum of the digits before and after the decimal point. The second number (here 1) is the scale – the number of decimal places. To work out how many digits fit before the decimal point, just subtract the second number from the first, that is 2-1 = 1. That is why the first number cannot be 0 or even negative – you would get an error “Length or precision specification 0 is invalid.”.

Likewise, if the first number is 0, then from what we know, the second one must be equal to or smaller than the first. So if you set the first number to 1 and wanted to give the second one, say, 2, it would fail with the error “The scale (2) for column ‘r’ must be within the range 0 to 1.”.

Now that I have found the key terms by googling, you can verify what I have written here at https://stackoverflow.com/questions/2377174/how-do-i-interpret-precision-and-scale-of-a-number-in-a-database .

Categories

Leave a Reply