Lucky Makhado
Lucky Makhado

Reputation: 107

Converting to decimal using sql statement

i have decimal numbers like in the table below, im trying to convert them to decimal, because when i select them, they are come out as integer, for example on row 1 if i select 0.8 or 2.86 it will give me 0 or 2 respectively

table in the database

im using the following statement

select ELERENVPR, ELEDCSFPM from db-name 

i tried cast to convert it

select cast(ELERENVPR, decimal), cast(ELEDCSFPM,decimal) from db-name 

im not sure if it works, becouse now its renaming the table to 00001 and 00002, like the picture below

enter image description here

Upvotes: 0

Views: 482

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269443

decimal with no scale or precision parameters has defaults, which might be to zero precision.

You need to be explicit about what you want. I would recommend something like decimal(10, 2) (8 digits before the decimal place, 2 after). Something like this:

select cast(ELERENVPR, decimal(10, 2)) as ELERENVPR,
       cast(ELEDCSFPM, decimal(10, 2)) as ELEDCSFPM
from db-name ;

The as renames the columns.

Upvotes: 2

Related Questions