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

dba.stackexchange https://dba.stackexchange.com/questions/260176

  •  24-02-2021
  •  | 
  •  

Pergunta

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)

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a dba.stackexchange
scroll top