Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change data type of a column in an SQL table from integer to decimal

I have assigned the data type of one of the columns of a table I have created as int. My problem is that it is not showing decimal places when I run a query against it.

How do I correct the data type of this column so that it accepts decimal places?

Table is dbo.Budget and the column concerned is called ROE.

like image 485
user3115933 Avatar asked Nov 27 '14 16:11

user3115933


People also ask

How do you change the datatype of a column with data?

Change data types in Design view If you do not have the table open, in the Navigation Pane, right-click the table that you want to change, and then click Design View on the shortcut menu. Locate the field that you want to change, and select a new data type from the list in the Data Type column. Save your changes.

Can INT in SQL have decimals?

For example, a type of INT which stands for integer in SQL server can only accept whole numbers, decimal values are not allowed.

Can you convert data types in SQL?

Data types can be converted either implicitly or explicitly. Implicit conversions are not visible to the user. SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds.


2 Answers

Easy - just run this SQL statement

ALTER TABLE dbo.Budget
ALTER COLUMN ROE DECIMAL(20,2)   -- or whatever precision and scale you need.....

See the freely available MSDN documentation on what exactly the precision, scale and length in decimal numbers are and what ranges of values are possible

like image 60
marc_s Avatar answered Oct 15 '22 18:10

marc_s


You can execute this simple sql statement

Alter table yourtable
Alter Column yourtable_column Decimal(10,2)

you can set decimal precision and scale whatever you need.

like image 22
HaveNoDisplayName Avatar answered Oct 15 '22 17:10

HaveNoDisplayName