SQL decimal field size

Asked

Viewed 1,334 times

6

I am making a form, and in the database documentation there are some numerical fields that are divided into 2 parameters. Ex:

Field: Cost

Type: NUMERIC

Tam.: (12,4)

What does this size divide and what size I would have to validate in the front?

2 answers

6


It may depend on the database you are using (originally you did not have this information in the question).

This is a numeric type that allows decimal places with accuracy, so it can be used for monetary value or another type of value that needs to be represented exactly.

Has database allows an accuracy of 10 to the 38, so are very large numbers.

Examples:

123.4567
12345678.9012

In general these numbers are written as integers (in this case an integer that has 12 digits). And it indicates how many houses you should apply the comma, which is called a scale, so what is actually recorded in the first case is the number 1234567, and it is shown or used for calculation by dividing by 10000 (4 zeros). But that’s detail that doesn’t matter to you.

You can record numbers that do not fit this feature in some databases, others may prevent you from writing a number with more digits or more decimals, some may make adjustments on their own, so you should consult the documentation to know what the mechanism you are using adopts and decide how to operate.

SQL Server prevents writing values outside of this standard so validating before can be interesting, I would do in almost every case one way or another (it may be that the input of the data already prevents a wrong value).

So I always say that people are under the illusion that one day they can exchange the database for another if they create an extra layer, there’s a lot of detail that needs to be different, you can only abstract everything if you make the lowest common denominator, which destroys performance. There are people who will speak ill of relational DB, think microservice is better and adopt other nonsense just because she makes a wrong decision before.

  • Something I thought should never happen on SQL-Server: if I reported more decimals than is required for storage, I expected it to just truncate/round up the extra houses. When by some misfortune a number with 30 decimal places was generated, SQL-Server complained and I needed to "truncate at hand" for a maximum of 25 houses. Day-to-day gambiarras

  • I just edited, to num sql server

  • @Jeffersonquesado I prefer the error in case so, because you may not want that, be explicit is better.

2

This divided size means the entire and fractional portion. It is an indicated way to record monetary records. Then the value would be stored in the database like this:

COST 57.9870

About validating on front-end will depend on business requirements.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.