Trying to round a value which results from 2 multiplied fields












2














In SQL Server, I am trying to round a value which results from 2 multiplied fields t.hourlyRate and t.hours. The sum is calculated as:



SUM(t.hours * t.hourlyRate) AS tot_cost


The results gives me values such 33.2330 or 51.6648 whereas I just want 33.2 or 51.7.



I've just variants of cast/round etc to no avail - any ideas please










share|improve this question
























  • Are you using decimal or float data type?
    – Salman A
    Nov 19 '18 at 15:48
















2














In SQL Server, I am trying to round a value which results from 2 multiplied fields t.hourlyRate and t.hours. The sum is calculated as:



SUM(t.hours * t.hourlyRate) AS tot_cost


The results gives me values such 33.2330 or 51.6648 whereas I just want 33.2 or 51.7.



I've just variants of cast/round etc to no avail - any ideas please










share|improve this question
























  • Are you using decimal or float data type?
    – Salman A
    Nov 19 '18 at 15:48














2












2








2







In SQL Server, I am trying to round a value which results from 2 multiplied fields t.hourlyRate and t.hours. The sum is calculated as:



SUM(t.hours * t.hourlyRate) AS tot_cost


The results gives me values such 33.2330 or 51.6648 whereas I just want 33.2 or 51.7.



I've just variants of cast/round etc to no avail - any ideas please










share|improve this question















In SQL Server, I am trying to round a value which results from 2 multiplied fields t.hourlyRate and t.hours. The sum is calculated as:



SUM(t.hours * t.hourlyRate) AS tot_cost


The results gives me values such 33.2330 or 51.6648 whereas I just want 33.2 or 51.7.



I've just variants of cast/round etc to no avail - any ideas please







sql sql-server tsql numbers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 10:03









Salman A

176k66336424




176k66336424










asked Nov 19 '18 at 14:45









matt80

184




184












  • Are you using decimal or float data type?
    – Salman A
    Nov 19 '18 at 15:48


















  • Are you using decimal or float data type?
    – Salman A
    Nov 19 '18 at 15:48
















Are you using decimal or float data type?
– Salman A
Nov 19 '18 at 15:48




Are you using decimal or float data type?
– Salman A
Nov 19 '18 at 15:48












4 Answers
4






active

oldest

votes


















2














You can cast() the result like this



cast(sum(t.hours * t.hourlyRate) as decimal(10,1))





share|improve this answer





















  • Thanks - this answered my query.
    – matt80
    Nov 19 '18 at 15:50



















2














Use ROUND function, which starts with SQL Server 2008 , upto precision 1 as :



SELECT ROUND(33.2330, 1) AS RoundValue1,
ROUND(51.6648, 1) AS RoundValue2;

RoundValue1 RoundValue2
----------- -----------
33.2 51.7





share|improve this answer





























    0














    If you are using SQL Server 2012 or later, then FORMAT is an option:



    FORMAT(SUM(t.hours * t.hourlyRate), 'N1') AS tot_cost





    share|improve this answer





























      0














      Use one of the following depending on whether you want to round individual values or the overall sum:



      SELECT SUM(CAST(t.hourlyRate * t.hours AS DECIMAL(10, 1))) AS tot_cost
      SELECT CAST(SUM(t.hourlyRate * t.hours) AS DECIMAL(10, 1)) AS tot_cost


      Both could produce sightly different results.






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377041%2ftrying-to-round-a-value-which-results-from-2-multiplied-fields%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        You can cast() the result like this



        cast(sum(t.hours * t.hourlyRate) as decimal(10,1))





        share|improve this answer





















        • Thanks - this answered my query.
          – matt80
          Nov 19 '18 at 15:50
















        2














        You can cast() the result like this



        cast(sum(t.hours * t.hourlyRate) as decimal(10,1))





        share|improve this answer





















        • Thanks - this answered my query.
          – matt80
          Nov 19 '18 at 15:50














        2












        2








        2






        You can cast() the result like this



        cast(sum(t.hours * t.hourlyRate) as decimal(10,1))





        share|improve this answer












        You can cast() the result like this



        cast(sum(t.hours * t.hourlyRate) as decimal(10,1))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 14:46









        juergen d

        159k24200260




        159k24200260












        • Thanks - this answered my query.
          – matt80
          Nov 19 '18 at 15:50


















        • Thanks - this answered my query.
          – matt80
          Nov 19 '18 at 15:50
















        Thanks - this answered my query.
        – matt80
        Nov 19 '18 at 15:50




        Thanks - this answered my query.
        – matt80
        Nov 19 '18 at 15:50













        2














        Use ROUND function, which starts with SQL Server 2008 , upto precision 1 as :



        SELECT ROUND(33.2330, 1) AS RoundValue1,
        ROUND(51.6648, 1) AS RoundValue2;

        RoundValue1 RoundValue2
        ----------- -----------
        33.2 51.7





        share|improve this answer


























          2














          Use ROUND function, which starts with SQL Server 2008 , upto precision 1 as :



          SELECT ROUND(33.2330, 1) AS RoundValue1,
          ROUND(51.6648, 1) AS RoundValue2;

          RoundValue1 RoundValue2
          ----------- -----------
          33.2 51.7





          share|improve this answer
























            2












            2








            2






            Use ROUND function, which starts with SQL Server 2008 , upto precision 1 as :



            SELECT ROUND(33.2330, 1) AS RoundValue1,
            ROUND(51.6648, 1) AS RoundValue2;

            RoundValue1 RoundValue2
            ----------- -----------
            33.2 51.7





            share|improve this answer












            Use ROUND function, which starts with SQL Server 2008 , upto precision 1 as :



            SELECT ROUND(33.2330, 1) AS RoundValue1,
            ROUND(51.6648, 1) AS RoundValue2;

            RoundValue1 RoundValue2
            ----------- -----------
            33.2 51.7






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 14:49









            Barbaros Özhan

            12.3k71530




            12.3k71530























                0














                If you are using SQL Server 2012 or later, then FORMAT is an option:



                FORMAT(SUM(t.hours * t.hourlyRate), 'N1') AS tot_cost





                share|improve this answer


























                  0














                  If you are using SQL Server 2012 or later, then FORMAT is an option:



                  FORMAT(SUM(t.hours * t.hourlyRate), 'N1') AS tot_cost





                  share|improve this answer
























                    0












                    0








                    0






                    If you are using SQL Server 2012 or later, then FORMAT is an option:



                    FORMAT(SUM(t.hours * t.hourlyRate), 'N1') AS tot_cost





                    share|improve this answer












                    If you are using SQL Server 2012 or later, then FORMAT is an option:



                    FORMAT(SUM(t.hours * t.hourlyRate), 'N1') AS tot_cost






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 19 '18 at 14:47









                    Tim Biegeleisen

                    218k1387140




                    218k1387140























                        0














                        Use one of the following depending on whether you want to round individual values or the overall sum:



                        SELECT SUM(CAST(t.hourlyRate * t.hours AS DECIMAL(10, 1))) AS tot_cost
                        SELECT CAST(SUM(t.hourlyRate * t.hours) AS DECIMAL(10, 1)) AS tot_cost


                        Both could produce sightly different results.






                        share|improve this answer




























                          0














                          Use one of the following depending on whether you want to round individual values or the overall sum:



                          SELECT SUM(CAST(t.hourlyRate * t.hours AS DECIMAL(10, 1))) AS tot_cost
                          SELECT CAST(SUM(t.hourlyRate * t.hours) AS DECIMAL(10, 1)) AS tot_cost


                          Both could produce sightly different results.






                          share|improve this answer


























                            0












                            0








                            0






                            Use one of the following depending on whether you want to round individual values or the overall sum:



                            SELECT SUM(CAST(t.hourlyRate * t.hours AS DECIMAL(10, 1))) AS tot_cost
                            SELECT CAST(SUM(t.hourlyRate * t.hours) AS DECIMAL(10, 1)) AS tot_cost


                            Both could produce sightly different results.






                            share|improve this answer














                            Use one of the following depending on whether you want to round individual values or the overall sum:



                            SELECT SUM(CAST(t.hourlyRate * t.hours AS DECIMAL(10, 1))) AS tot_cost
                            SELECT CAST(SUM(t.hourlyRate * t.hours) AS DECIMAL(10, 1)) AS tot_cost


                            Both could produce sightly different results.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 20 '18 at 10:09

























                            answered Nov 19 '18 at 15:41









                            Salman A

                            176k66336424




                            176k66336424






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377041%2ftrying-to-round-a-value-which-results-from-2-multiplied-fields%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                'app-layout' is not a known element: how to share Component with different Modules

                                android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                WPF add header to Image with URL pettitions [duplicate]