Error converting data type varchar to float

Sometimes, under certain circumstances, when you develop in SQL Server and especially when you try to convert a string data type value to a float data type value, you might get the error message: error converting data type varchar to float. As the error message describes, there is a conversion error and this is most probably due to the input parameter value you used in the conversion function.

Read more below on how you can easily resolve this problem.

 

Reproducing the Data Type Conversion Error

As mentioned above, the actual reason you get this error message, is that you are passing as a parameter to the CAST or CONVERT SQL Server functions, a value (varchar expression) that is invalid and cannot be converted to the desired data type.

Consider the following example:

-----------------------------------------
--Variable declaration and initialization
-----------------------------------------
DECLARE @value AS VARCHAR(50);

SET @value = '12.340.111,91';

--Perform the casting
SELECT CAST(@value AS FLOAT);

--or
--Perform the conversion
SELECT CONVERT(FLOAT, @value);
-----------------------------------------

If you execute the above code you will get an error message in the following type:

Msg 8114, Level 16, State 5, Line 6
Error converting data type varchar to float.

Another similar example where you get the same data type conversion error, is the below:

-----------------------------------------
--Variable declaration and initialization
-----------------------------------------
DECLARE @value2 AS VARCHAR(50);

SET @value2 = '12,340.15';

--Perform the casting
SELECT CAST(@value2 AS FLOAT);

--or
--Perform the conversion
SELECT CONVERT(FLOAT, @value2);

 

Why you get this Conversion Error

The exact reason for getting the error message in this case is that you are using the comma (,) as a decimal point and also the dots as group digit symbols. Though SQL Server considers as a decimal point the dot (.). Also when converting a varchar to float you must not use any digit grouping symbols.

 


Strengthen your SQL Server Administration Skills – Enroll to our Online Course!

Check our online course on Udemy titled “Essential SQL Server Administration Tips
(special limited-time discount included in link).

Via the course, you will learn essential hands-on SQL Server Administration tips on SQL Server maintenance, security, performance, integration, error handling and more. Many live demonstrations and downloadable resources included!

Essential SQL Server Administration Tips - Online Course with Live Demonstrations and Hands-on Guides
(Lifetime Access/ Live Demos / Downloadable Resources and more!)

Learn More


How to Resolve the Conversion Issue

In order for the above code to execute, you would need to first remove the dots (that is the digit grouping symbols in this case) and then replace the comma with a dot thus properly defining the decimal symbol for the varchar expression.

Note: You need to be careful at this point, in order to correctly specify the decimal symbol at the correct position of the number.

Therefore, you can modify the code of example 1 as per below example:

-------------------------------------------
--Variable declaration and initialization
-------------------------------------------
DECLARE @value AS VARCHAR(50);

SET @value = '12.340.111,91';

--Prepare the string for casting/conversion to float
SET @value = REPLACE(@value, '.', '');
SET @value = REPLACE(@value, ',', '.');

--Perform the casting
SELECT CAST(@value AS FLOAT);

--or
--Perform the conversion
SELECT CONVERT(FLOAT, @value);
-----------------------------------------

If you execute the above code you will be able to get the string successfully converted to float.

Similarly, you can modify the code of example 2 as per below example:

-----------------------------------------
--Variable declaration and initialization
-----------------------------------------
DECLARE @value2 AS VARCHAR(50);

SET @value2 = '12,340.15';

--Prepare the string for casting/conversion to float
SET @value2 = REPLACE(@value2, ',', '');

--Perform the casting
SELECT CAST(@value2 AS FLOAT);

--or
--Perform the conversion
SELECT CONVERT(FLOAT, @value2);

Again, if you execute the above code you will be able to get the string successfully converted to float.

*Note: Even though you can try changing the regional settings of the PC for setting the dot (.) as the decimal symbol, this will only affect the way the data is presented to you when returned from the casting/conversion call. Therefore, you still have to modify the varchar expression prior to the casting/conversion operation.

 

Regarding the message: error converting data type varchar to numeric

The above error message is similar to the one we examined in this article, therefore, the way for resolving the issue is similar to the one we described in the article. The only different for the numeric case, is that you will have to replace FLOAT with numeric[ (p[ ,s] )]. Learn more about the numeric data type in SQL Server and how to resolve the above conversion issue, by reading the relevant article on SQLNetHub.

 

Watch the Live Demonstration on the VARCHAR to FLOAT Data Type Conversion Error

 

 

Learn essential SQL Server development tips! Enroll to our Online Course!

Check our online course titled “Essential SQL Server Development Tips for SQL Developers
(special limited-time discount included in link).

Sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!

Essential SQL Server Development Tips for SQL Developers - Online Course
(Lifetime Access, Certificate of Completion, downloadable resources and more!)

Learn More

 

Featured Online Courses:

 

Read Also:

 

Recommended Software Tools

Snippets Generator: Create and modify T-SQL snippets for use in SQL Management Studio, fast, easy and efficiently.

Snippets Generator - SQL Snippets Creation Tool

Learn more

 

Dynamic SQL Generator: Convert static T-SQL code to dynamic and vice versa, easily and fast.

Dynamic SQL Generator: Easily convert static SQL Server T-SQL scripts to dynamic and vice versa.

Learn more

 


Get Started with Programming Fast and Easy – Enroll to the Online Course!

Check our online course “Introduction to Computer Programming for Beginners
(special limited-time discount included in link).

The Philosophy and Fundamentals of Computer Programming - Online Course - Get Started with C, C++, C#, Java, SQL and Python
(Lifetime Access, Q&A, Certificate of Completion, downloadable resources and more!)

Learn the philosophy and main principles of Computer Programming and get introduced to C, C++, C#, Python, Java and SQL.

Learn More


 

Subscribe to our newsletter and stay up to date!

Subscribe to our YouTube channel (SQLNetHub TV)

Easily generate snippets with Snippets Generator!

Secure your databases using DBA Security Advisor!

Generate dynamic T-SQL scripts with Dynamic SQL Generator!

Check our latest software releases!

Check our eBooks!

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (17 votes, average: 5.00 out of 5)

Loading...

Reference: SQLNetHub.com (https://www.sqlnethub.com)

© SQLNetHub



How to resolve the error: Error converting data type varchar to float Click to Tweet

10 thoughts on “Error converting data type varchar to float”

  1. I need to convert nvarchar datatype of my table column to float

    But during this operation

    the below error message returned by the sql server 2005

    'tbl_user_mast' table – Unable to modify table. Error converting data type nvarchar to float.

  2. Hi )
    Can you help a beginner with StoredProcedure? )

    I have a table with columns (type varchar) as example:
    D1-D2-D3
    3 – б –
    2 – – 8
    1 – –
    I need to get the sum of each column
    if only there were numbers – then everything is OK
    write

    SELECT
    SUM (CAST (Round (D1, 2) As decimal (5,2))) As D1
    FROM

    but i have a letters in some columns, so can't convert string to float

    (if in columns is a letter,then SUM is equivalent to = 8 )

    so i try to use CASE


    SELECT
    SumD2 =
    CASE D2
    WHEN 'б' then 8
    ELSE
    SUM (CAST (Round (D2, 2) As decimal (5,2)))
    END
    FROM

    and here's a "gift" – Column dbo.Zadanie.D2 is invalid in the select list because it is not contained in either an aggregate function or ORDER BY clause

    tell my what am I doing wrong?

  3. Hi Elena,

    You are getting this error because you are using an aggregate (SUM) in the one part of the CASE statement and you are not using an aggregate in the other part.

    You could use a GROUP BY or an ORDER BY clause but you would not get the result you expect.

    It is difficult to implement the requested logic with a single T-SQL statement because queries read record-by-record the contents of a table. Whenever a character occurs you would need to stop the aggregation and set the sum to 8.

    Though, by the time you are using a stored procedure as you said, you can use multiple statements for implementing the required logic.

    To this end I would suggest trying the following code in the stored procedure you are building:

    DECLARE @d1 AS INT
    DECLARE @d2 AS INT
    DECLARE @d3 AS INT

    DECLARE @sumd1 AS FLOAT
    DECLARE @sumd2 AS FLOAT
    DECLARE @sumd3 AS FLOAT

    SET @d1=(SELECT COUNT(*)
    FROM testtable
    WHERE Isnumeric(d1) = 0
    AND d1 IS NOT NULL)

    SET @d2=(SELECT COUNT(*)
    FROM testtable
    WHERE Isnumeric(d2) = 0
    AND d2 IS NOT NULL)

    SET @d3=(SELECT COUNT(*)
    FROM testtable
    WHERE Isnumeric(d3) = 0
    AND d3 IS NOT NULL)

    IF @d1 = 0
    SET @sumd1=(SELECT SUM(CAST(Round (d1, 2) AS DECIMAL (5, 2)))
    FROM testtable)
    ELSE
    SET @sumd1=8

    SELECT @sumd1

    IF @d2 = 0
    SET @sumd2=(SELECT SUM(CAST(Round (d2, 2) AS DECIMAL (5, 2)))
    FROM testtable)
    ELSE
    SET @sumd2=8

    SELECT @sumd2

    IF @d3 = 0
    SET @sumd3=(SELECT SUM(CAST(Round (d3, 2) AS DECIMAL (5, 2)))
    FROM testtable)
    ELSE
    SET @sumd3=8

    SELECT @sumd3

    If the table contained an ID column along with the value columns maybe another logic could be implemented but by the time the table contains only value columns the above is the workaround I would suggest.

    Cheers,
    Artemis

  4. Thank you for your quick and detailed response so)
    I did so:

    SELECT

    SUM(CASE D1 WHEN 'б' THEN 8 WHEN 'о' THEN 8 WHEN 'от' THEN 8 WHEN 'к' THEN 8 ELSE CAST(ROUND(D1,2) AS DECIMAL(5,2)) END) as SumD1,
    SUM(CASE D2 WHEN 'б' THEN 8 WHEN 'о' THEN 8 WHEN 'от' THEN 8 WHEN 'к' THEN 8 ELSE CAST(ROUND(D2,2) AS DECIMAL(5,2)) END) as SumD2,
    SUM(CASE D3 WHEN 'б' THEN 8 WHEN 'о' THEN 8 WHEN 'от' THEN 8 WHEN 'к' THEN 8 ELSE CAST(ROUND(D3,2) AS DECIMAL(5,2)) END) as SumD3,

    FROM dbo.PrZad

    WHERE (StaffID = @StaffId) AND (MONTH(PlanDataOtch) = @Месяц) AND (YEAR(PlanDataOtch) = @Год) AND (CodZak <> 24)

    It works!
    Thank you!!

  5. I also incurred same error msg "Error converting data type nvarchar to numeric", solved converting numeric column to varchar

    Problem: TableA.EmpID = TableB.EmployeeID

    Here TableA.EmpID is varchar datatype and in other end TableB.EmployeeID was in Decimal datatype. I converted TableB.EmployeeID to varchar datatype.

    Solution: TableA.EmpID = Cast (TableB.EmployeeID as Varchar)

Comments are closed.