Search code examples
sqlsybase

How to do explicit conversion of FLOAT to a VARCHAR field in SQL


I'm getting a query from a column float (with no precision) and inserting it in another table and comumn float (with no precision as well) but I'm getting this error:

Error (265) Insufficient result space for explicit conversion of FLOAT value '3.8833137793643' to a VARCHAR field.

The query:

INSERT INTO TableA
SELECT DISTINCT max(price_1) AS PriceValue
FROM TableB

Solution

  • This does may or may not answer your question. But the query should be written as:

    INSERT INTO TableA (<column name>)
        SELECT MAX(price_1) AS PriceValue
        FROM TableB;
    

    Notes:

    • An aggregation query with no GROUP BY returns exactly one row. SELECT DISTINCT is not necessary.
    • You should include the columns being inserted.

    Your problem is clearly that the column is not wide enough. If you have defined the table as:

    create table tableA (
        col varchar
    );
    

    Then you have not specified a length and are depending on the default in the database. Do you even know what the default is? In this case, it is 1. And that is not long enough for your value. You just need to provide a long-enough length:

    create table tableA (
        col varchar(255)
    );
    

    All that said. I strongly discourage you from storing number values as strings. That is likely to create problems now and in the future. Use the appropriate types for your data.