converting fahrenheit to celsius

  • I'm knew to Sql, I've tried many variations, and I keep getting errors, assistance would be greatly appreciated;

    CAST(ROUND(IP_BLa.MEAS_VALUE-32)*5/9,2)

    CAST(ROUND(IP_BLa.MEAS_VALUE-32)*5/9,2)as decimal(3,2))

  • You need to pay more attention to details.

    You are missing one parenthesis in your second option and the target data type in the first one.

    The precision in decimal indicates the full number of digits, not only the ones on the left.

    Finally, you want to be sure to prevent integer division for accurate results.

    CREATE TABLE IP_BLa(

    MEAS_VALUE int)

    INSERT INTO IP_BLa

    VALUES(50), (60), (70), (80), (90), (100);

    --Added one parenthesis and used a larger decimal precision

    SELECT CAST(ROUND((IP_BLa.MEAS_VALUE-32.)*5/9,2) as decimal(6,2)), --using decimals by adding a point to 32 (converts it to float)

    CAST(ROUND((IP_BLa.MEAS_VALUE-32)*5/9,2)as decimal(6,2)) --using integer division

    FROM IP_BLa

    GO

    DROP TABLE IP_BLa

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply