Sql-server – Convert a numeric column with number eg.(-1222222) to two float column with two decimal places eg (-12222.22)

castfloating pointsql server

I know there are different variations of this question asked.
But what I am trying to do is that, there is a table with a numeric column (12,0)
that has data looking like

Units
-----
-1222222
0
-19
21123021

What they really mean is this

Units
-----
-12222.22
0.00
-0.19
211230.21

With two decimal places in the last digits of each number.

I am struggling to convert these numbers to a two decimal place float column(Units)

Best Answer

Something like this will do:

with data as 
(
select -1222222 as foo 
union
select 0
union 
select -19
union 
select 21123021
)
select cast(cast(foo as decimal)/100 as decimal(10,2))
from data;

You can also use format(foo,'N2') and a million other ways.