Link to home
Start Free TrialLog in
Avatar of Sashaski
Sashaski

asked on

Error when trying to change data type from nvarchar to int in SQL Server 2008

I am brand new to SQL so please respond in very simple steps.

This is what I tried:

Alter Table MY TABLE NAME
Alter Column MY COLUMN NAME int      

This is the error message I am receiving

(5429 row(s) affected)
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the nvarchar value '1,500' to data type int.
The statement has been terminated.

I have 5,429 records which contain numbers and NULL values.

My goal is to use this field as part of a calculation in an access form.

Thank you.

Avatar of wdosanjos
wdosanjos
Flag of United States of America image

You need to remove the commas in the column values before you can alter it to an int.  Something like this:

update MyTable set MyColumn = replace(MyColumn, ',', '')
where MyColumn is not null

I hope this helps.
Avatar of Sashaski
Sashaski

ASKER

Thank you wdosanjos...that worked for removing the , now i have a new error message (sorry, I didn't realize I had decimals in this column either...this data was imported from a spreadsheet originally)..

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value '95.6' to data type int.
The statement has been terminated.
What do you want to do with the decimals (truncate, round)?  To avoid data loss, you should consider altering the column to decimal or money data type.
I need to keep the decimals in place for this field.  This field will be calculated from another field of int type which at times will also have numbers past the decimal.
OK. Then you should alter the column to decimal instead of int as follows (adjust the number of decimal digits based on your data):

Alter Table MYTABLE
Alter Column MYCOLUMN decimal(18,3) -- where 3 is the max number of decimal digits
Just tried that (only changing the 3 to a 2 for my case)...this is the error message I received:

(5429 row(s) affected)
Msg 8114, Level 16, State 5, Line 2
Error converting data type nvarchar to numeric.
The statement has been terminated.
I suspect you still have some non-numeric values in the column (perhaps spaces).  Please try the following clean up:

update mytable set mycolumn = replace(mycolumn, ' ', '')
update mytable set mycolumn = case when mycolumn = '' then null else mycolumn end
You are probably correct on the non-numeric values.  I executed the query above, it has been running for almost 6 minutes...no results yet.
ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes...my rookie move!  I was working on a form in Access which is linked to the table I was running the query on.  Oops...

Command(s) completed successfully!  Data type is now a decimal.

Do you suggest I do the same with the int data type that will be used in a calculation with the field you just helped me fix?

Thank you...thank you...thank you!
Your quick, easy to follow and patient response to my problem is greatly appreciated. I've been a long time 'searcher' of EE for a while, this was my first time asking a question.  What an awesome experience!   Thank you so much wdosanjos!