Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everyone,

I have a column [Weight] whose datatype is varchar(50) now i want to alter the column in decimal(10,2).I try this .

ALTER TABLE MAWB
ALTER COLUMN [Weight] decimal(10,2)
I always get the error: Error converting data type varchar to numeric.

Please let me know why do i get this error.

thanks in advance
Harshal.
Posted
Comments
Sergey Alexandrovich Kryukov 18-Aug-14 3:45am    
If you need numeric data, use attributes of numeric types, not varchar.
—SA
Wombaticus 18-Aug-14 3:46am    
Check your data integrity - perhapos set all empty string values to 0 first.
R Harshal 18-Aug-14 3:48am    
Sorry to say ,I did not get it .
what exactly i need to do ??
Wombaticus 18-Aug-14 4:12am    
execute
UPDATE MAWB SET Weight = '0' WHERE Weight = ''
before your ALTER statement.
Magic Wonder 18-Aug-14 3:49am    
Check your table's column data should not contain VARCHAR value.

This is a good idea, but...your table data is already corrupt.
At least one row in your DB contains non-numeric data and SQL can't convert it. Until that is fixed, you can't alter the column datatype, because SQL doesn't know what to do with the row, and it doesn't want to lose you data.

So, throw together some quick test code in C# or similar to read every row, and report to you which ones can't be converted to numeric values. Then you can fix the rows manually, and change your DB.
 
Share this answer
 
Hi,

You can achieve your task as listed below.



1. as you are doing,
SQL
ALTER TABLE tableName ALTER COLUMN [colName] (datatype)
provided that your data should be of required data type.

2. follow below steps

a. Add another column with required dataType.
b. import all data from old column to new column with dataType check.
c. delete / drop old column with the help of query / management studio once you imported all your required data.
d. Rename your new column as per requirement.


3. Recreate New Table with required table structure, import all required data, drop old table, rename new table as require.

Choice is yours.


Cheers
 
Share this answer
 
Comments
R Harshal 18-Aug-14 4:28am    
Thank you Magic Wonder.
Magic Wonder 18-Aug-14 4:55am    
Your welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900