Click here to Skip to main content
15,891,431 members
Articles / Programming Languages / SQL

How Do I Handle a “Error converting data type” Error?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
13 Mar 2019MIT1 min read 3.3K   3  
An answer to fix an error converting data type from varchar to float
A reader recently asked about an error converting data type error they received. Since this is a common issue, especially when numeric data is stored within VARCHAR datatypes, I thought you would appreciate the answer I shared with them.

I’m using Windows 10 and SQL SMS 2014. When I run a query, I get the following error message: 

Error converting data type varchar to float.”  Can you help in troubleshooting this error message?

Usually when SQL tries to convert a number, such as 1.25 from text to float, it can do so easily; however, it does get hung up and will throw an error converting data if there are some non-numeric characters in the values.

For example, converting 1.25 would work, but converting $1.25 would not, since the server wouldn’t know how to deal with the “$

My suspicion is that you have some data in the column you’re trying to convert that isn’t “purely” numeric.

If you go through the data, may be you see some alphabetical data there.

Here are some examples you can try in the query editor:

SQL
--EXAMPLES
DECLARE @myText as varchar(20)
DECLARE @myFloat as Money

--This Works!  :)
Print 'Try 1.20 * 1.10'
SET @myText = '1.20'
SET @myFloat = @myText * 1.10
Print @myFloat

--This Will throw an error   :(
PRINT 'Try $1.20 * 1.10'
SET @myText = '$1.20'
SET @myFloat = @myText * 1.10
Print @myFloat

Depending on what you’re trying to do with your data, such as using it in a query, you may have to use an IIF function in conjunction with ISNUMERIC to test if the column value can be converted, and if not, then display another suitable value.

The ISNUMERIC function returns 1 if the value tested is numeric; otherwise 0. It can be used to test up-front whether characters can be tested to numeric data types.

Here is an example:

SQL
SELECT IIF(ISNUMERIC(myTexttoFloatColumn) = 1,
           myTextToFloatColumn*1.50, 
           myTexttoFloatColumn)
FROM   mySampleTable

This would either multiply the column by 1.5, if the column can be converted to a number, or just display the original value…

Hope this helps!

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Easy Computer Academy, LLC
United States United States
Hello my name is Kris. I’m here because I am passionate about helping non-techie people to overcome their fear of learning SQL.

I know what it is like to not know where to start or whether the time spent learning is worth the effort. That is why I am here to help you to:
- Get started in an easy to follow step-by-step manner.
- Use your time wisely so you focus on what is important to learn to get the most value from your time.
- Answer your questions. Really! Just post a comment and I’ll respond. I’m here to help.

It wasn’t long ago that I was helping a colleague with some reporting. She didn’t know where to start and soon got overwhelmed and lost as she didn’t know SQL.

I felt really bad, as she was under pressure to get some summary information to her boss, the built-in reports were falling short, and to make them better would require her to know SQL. At that time that seemed impossible! It in dawned on me, it doesn’t have to be that way.

Then I discovered a way for anyone with the desire to easily learn SQL. I worked with my co-worker, started to teach her what I learned and soon she was able to write reports and answer her boss’ questions without getting stressed or ploughing hours into manipulating data in Excel.

It hasn’t always been easy. Sometimes the information seems abstract or too conceptual. In this case I’ve found out that a visual explanation is best. I really like to use diagrams or videos to explain hard-to-grasp ideas.

Having video, pictures, and text really help to reinforce the point and enable learning.

And now I want to help you get the same results.

The first step is simple, click here http://www.essentialsql.com/get-started-with-sql-server/

Comments and Discussions

 
-- There are no messages in this forum --