Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
1.1k views
in SQL Queries by 21 20 26
edited by

I created this table but I forgot and set the data type for the price column  as nvarchar

Create table Product
(
 ID int primary key identity,
 ProductName nvarchar(50),
 Price nvarchar(50)
)

I want to alter datatype for column price from nvarchar to int  how can I do that?


1 Answer

0 like 0 dislike
by 22 25 39
edited by

Change data column type in SQL Server

To modify the data type of a column you can use two ways:

1) using SQL Server Management Studio

In object explorer - right click on Product table - click design
Select the column that you want to modify data type-change Price column data type from Nvarchar to int- and click save table

  • In object explorer- right click on Product table - click design

Design in SQL

  • Modify data type-change Price column data type from Nvarchar to int- and click save

Change data column type in SQL

Note You can not Alter column from string to integer if the column holding string data

2) Using Alter Query

ALTER TABLE Product 
ALTER COLUMN Price int 

More information at change data type for column

by 26 29 35
0 0
What if the current column already hold string and you want to change it to int! Is it working??
by 22 25 39
0 0
If the column already hold string you will get  this error
Conversion failed when converting the nvarchar value  to data type int.
I update my answer now thanks for your comment
If you don’t ask, the answer is always NO!
...