?
Solved

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

Posted on 2011-09-06
11
Medium Priority
?
333 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
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

850 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