Cast syntax to convert a sum to float

90,681

Solution 1

You need to use the cast syntax:

SELECT CAST (SUM(Seconds) AS FLOAT)/-1323 AS Averag;

Solution 2

I use the shorthand cast syntax almost everywhere:

SELECT sum(seconds)::float / -1323 AS averag;

More details:

Solution 3

It is not exact casting but a trick to do the job :) and works almost in any language.

SELECT SUM(Seconds)/-1323.0 AS Averag;

OR

SELECT SUM(Seconds)*1.0/-1323 AS Averag;

Share:
90,681
MAK
Author by

MAK

Database Developer (Microsoft SQL Server and PostgreSQL).

Updated on May 28, 2021

Comments

  • MAK
    MAK almost 3 years

    Using PostgreSQL 9.3, I want to convert the calculated values to data type float.

    My first attempt:

    SELECT float(SUM(Seconds))/-1323 AS Averag;
    

    Gives me this error:

    syntax error at or near "SUM"
    

    My second attempt:

    SELECT to_float(SUM(Seconds))/-1323 AS Averag;
    

    Gives me this error:

     function to_float(bigint) does not exist