Solved

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

Posted on 2011-09-06
11
325 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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