Solved

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

Posted on 2011-09-06
11
321 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
 

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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now