How to sum in a bigint instead of an int

  • I have a Query, where I SELECT SUM(A.MINUTES) from....

    The variable A.minutes is defined as an INT

    Problem is, that I now and Again gets an aritmic overflow here, because sum(A.Minutes) can be pretty huge

    So, How to get the sum into an BIGINT instead.

    To change 'A.Minutes' to BIGINT is not an option unfortunately

    Best regards

    Edvard Korsbæk

  • edvard 19773 (7/4/2013)


    I have a Query, where I SELECT SUM(A.MINUTES) from....

    The variable A.minutes is defined as an INT

    Problem is, that I now and Again gets an aritmic overflow here, because sum(A.Minutes) can be pretty huge

    So, How to get the sum into an BIGINT instead.

    To change 'A.Minutes' to BIGINT is not an option unfortunately

    Best regards

    Edvard Korsbæk

    I would try

    SUM(CAST(A.Minutes AS BIGINT))

    Godspeed

  • I would do the same!

    Works like a charm

    Thanks!

  • 🙂

  • This might work too.

    WITH BigInts (n) AS (

    SELECT CAST(9223372036854775807 AS BIGINT)

    UNION ALL SELECT CAST(9223372036854775807 AS BIGINT))

    SELECT SUM(CAST(n AS DECIMAL(38,0)))

    FROM BigInts


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

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

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