SQL Server — Short Demo with Decimal Precision also Float or Int Conversion

tanut aran
2 min readMay 6, 2024

--

Intro about Numeric Precision

create table test_change_to_dem (
id int identity,
num1 numeric(20,6),
num2 numeric(16,4),
num3 numeric(18,4)
)


insert into test_num (num1,num2,num3)
values
(200.11111111,200.11111111,200.11111111)
(300.22222222,300.22222222,300.22222222)
create table test_change_to_decimal (
id int identity,
myint int,
myfloat float
)

Then we try to multiple and see the result of precision

select num1*num2*num3 from test_num

Then we try to insert back to the table to observe precision and rounding

You can see the rounding of the exceeding decimal point

The Integer and Float to numeric


insert into test_change_to_dem (myint,myfloat)
values (300.22222222,300.22222222)

insert into test_num (num1,num2,num3)
values (300.22222222,300.22222222,300.22222222)

There is time when we assume some data is just int or float but have to change it later to decimal

We will also demo the result / loss of date type change from int and float to decimal

alter table test_change_to_decimal alter column myint decimal(15,2)
alter table test_change_to_decimal alter column myfloat decimal(15,2)

select * from test_change_to_decimal

Bonus: Converting back

The effect is limited when converting back to int and float

alter table test_change_to_decimal alter column myint int
alter table test_change_to_decimal alter column myfloat float

select * from test_change_to_decimal

--

--

tanut aran
tanut aran

Written by tanut aran

Co-founder and Coder at work !

No responses yet