?
Solved

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

Posted on 2011-09-06
11
Medium Priority
?
327 Views
Last Modified: 2012-05-12
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.

0
Comment
Question by:Sashaski
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 36491274
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.
0
 

Author Comment

by:Sashaski
ID: 36491336
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.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 36491376
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.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:Sashaski
ID: 36491494
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.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 36491531
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
0
 

Author Comment

by:Sashaski
ID: 36491562
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.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 36491637
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
0
 

Author Comment

by:Sashaski
ID: 36491701
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.
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 36491765
Perhaps you have rows locked in another window.
0
 

Author Comment

by:Sashaski
ID: 36491811
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!
0
 

Author Closing Comment

by:Sashaski
ID: 36491833
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!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question