Change the number of decimals in the table structure (SQL SERVER)

Asked

Viewed 65 times

0

Good Evening Everyone,

I have 3 fields in a table that use the "Data type" defined as "decimal(15, 2)" and I need to change (via code) to "decimal(15, 3)".

That is, change from 2 decimal places to 3.

I need to do it via code, because I have to change all my clients... and it would be impossible to change field by field in each client’s structure.

  • Have you tried using SQL management studio? It makes this script for you...

1 answer

0

You can use the command ALTER TABLE

ALTER TABLE NomeDaTabela ALTER COLUMN NomeDaColuna decimal(16,3);

See what I used here (16,3), because it already had (15,2), that is, it had "15 digits, being 2 reserved for the decimals" and if do (15,3) will lose an entire digit (before the point), may even receive a truncated data message, so gets "16 digits, being 3 for decimals", ie, continues with 15 integers (before the point).

To be clearer "visually":

<------ 15 ------>
NNNNNNNNNNNNNNN.NN  -> decimal (15,2)
NNNNNNNNNNNNNN.NNN  -> decimal (15,3) perde 1 dígito inteiro
  • Obg friend, solved beyond what needed

  • for nothing, don’t forget to choose the answer in this case ;)

Browser other questions tagged

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