Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Decimals in integer operations

Decimals in integer operations

Scheduled Pinned Locked Moved Database
databasequestionsql-serversysadmin
12 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shreekar
    wrote on last edited by
    #1

    Hi Database: SQL Server 2005 select 589/5 produces 117 How can I make it return 117.8? I am doing the division operation on 2 int columns and want the output in decimals. Do I have to make the columns themselves decimal? Thanks

    Shreekar

    S L 2 Replies Last reply
    0
    • S shreekar

      Hi Database: SQL Server 2005 select 589/5 produces 117 How can I make it return 117.8? I am doing the division operation on 2 int columns and want the output in decimals. Do I have to make the columns themselves decimal? Thanks

      Shreekar

      S Offline
      S Offline
      shreekar
      wrote on last edited by
      #2

      OK - I have tried the below:

      declare @d decimal(10,2)
      select @d = 859 * 1/5
      select @d

      This returns 171.00 and

      declare @d decimal(10,2)
      select @d = 859.00 * 1/5.00
      select @d

      This returns 171.80 This tells me that the 2 numbers participating in the operation have to be decimals by definition. Is there any way to work around this as I do not want to change the column types to decimals as I do not expect them to hold fraction data any time. It is only the result that may have fraction data. Any ideas?

      Shreekar

      L S D 3 Replies Last reply
      0
      • S shreekar

        OK - I have tried the below:

        declare @d decimal(10,2)
        select @d = 859 * 1/5
        select @d

        This returns 171.00 and

        declare @d decimal(10,2)
        select @d = 859.00 * 1/5.00
        select @d

        This returns 171.80 This tells me that the 2 numbers participating in the operation have to be decimals by definition. Is there any way to work around this as I do not want to change the column types to decimals as I do not expect them to hold fraction data any time. It is only the result that may have fraction data. Any ideas?

        Shreekar

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Cast[^] it;

        SELECT CAST(859 AS DECIMAL) / CAST(5 AS DECIMAL)

        I are Troll :suss:

        S 1 Reply Last reply
        0
        • S shreekar

          OK - I have tried the below:

          declare @d decimal(10,2)
          select @d = 859 * 1/5
          select @d

          This returns 171.00 and

          declare @d decimal(10,2)
          select @d = 859.00 * 1/5.00
          select @d

          This returns 171.80 This tells me that the 2 numbers participating in the operation have to be decimals by definition. Is there any way to work around this as I do not want to change the column types to decimals as I do not expect them to hold fraction data any time. It is only the result that may have fraction data. Any ideas?

          Shreekar

          S Offline
          S Offline
          shreekar
          wrote on last edited by
          #4

          Figured it out myself, I cast the operands themselves to decimals and the product was decimal. Sorry about that - turned out to be loud thinking than a genuine question.

          Shreekar

          D 1 Reply Last reply
          0
          • L Lost User

            Cast[^] it;

            SELECT CAST(859 AS DECIMAL) / CAST(5 AS DECIMAL)

            I are Troll :suss:

            S Offline
            S Offline
            shreekar
            wrote on last edited by
            #5

            Just missed your post. Realised the same solution as you suggested. Thanks for the confirmation!

            Shreekar

            L 1 Reply Last reply
            0
            • S shreekar

              Hi Database: SQL Server 2005 select 589/5 produces 117 How can I make it return 117.8? I am doing the division operation on 2 int columns and want the output in decimals. Do I have to make the columns themselves decimal? Thanks

              Shreekar

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              in most programming languages, when all numbers are integer, division will truncate. You can promote a (sub)expression to floating-point in several ways: by inserting a floating constant, by having at least one floating variable, by casting, by converting. As a simple experiment, I would prefix 1.0* to whatever expression you are having. Warning: this rule also applies to subexpressions, so 2/5 will be zero, whereas 1.0*2/5 would not! :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              S 1 Reply Last reply
              0
              • S shreekar

                OK - I have tried the below:

                declare @d decimal(10,2)
                select @d = 859 * 1/5
                select @d

                This returns 171.00 and

                declare @d decimal(10,2)
                select @d = 859.00 * 1/5.00
                select @d

                This returns 171.80 This tells me that the 2 numbers participating in the operation have to be decimals by definition. Is there any way to work around this as I do not want to change the column types to decimals as I do not expect them to hold fraction data any time. It is only the result that may have fraction data. Any ideas?

                Shreekar

                D Offline
                D Offline
                David Skelly
                wrote on last edited by
                #7

                declare @n integer
                declare @d decimal(10,2)
                select @n = 859
                select @d = CAST(@n AS decimal(10,2)) / 5
                select @d

                Returns 171.80

                1 Reply Last reply
                0
                • S shreekar

                  Figured it out myself, I cast the operands themselves to decimals and the product was decimal. Sorry about that - turned out to be loud thinking than a genuine question.

                  Shreekar

                  D Offline
                  D Offline
                  David Skelly
                  wrote on last edited by
                  #8

                  It's like buses. You stand around waiting for an answer, and then three come along all at the same time.

                  1 Reply Last reply
                  0
                  • S shreekar

                    Just missed your post. Realised the same solution as you suggested. Thanks for the confirmation!

                    Shreekar

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    You're welcome. Thinking out loud helps, even on a messageboard :)

                    I are Troll :suss:

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      in most programming languages, when all numbers are integer, division will truncate. You can promote a (sub)expression to floating-point in several ways: by inserting a floating constant, by having at least one floating variable, by casting, by converting. As a simple experiment, I would prefix 1.0* to whatever expression you are having. Warning: this rule also applies to subexpressions, so 2/5 will be zero, whereas 1.0*2/5 would not! :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                      S Offline
                      S Offline
                      shreekar
                      wrote on last edited by
                      #10

                      Multiplying by 1.0 is definitely easier on the eyes than multiple nested cast statements and produces the same output - at least in my case because there is only one operation. So thanks for that. Earlier, I had read posts alluding to this technique but I had missed the point - now I get it.

                      Shreekar

                      L D 2 Replies Last reply
                      0
                      • S shreekar

                        Multiplying by 1.0 is definitely easier on the eyes than multiple nested cast statements and produces the same output - at least in my case because there is only one operation. So thanks for that. Earlier, I had read posts alluding to this technique but I had missed the point - now I get it.

                        Shreekar

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        you're welcome. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                        1 Reply Last reply
                        0
                        • S shreekar

                          Multiplying by 1.0 is definitely easier on the eyes than multiple nested cast statements and produces the same output - at least in my case because there is only one operation. So thanks for that. Earlier, I had read posts alluding to this technique but I had missed the point - now I get it.

                          Shreekar

                          D Offline
                          D Offline
                          David Skelly
                          wrote on last edited by
                          #12

                          One word of warning with this technique, the following give different results:

                          859 / 5 * 1.0
                          1.0 * 859 / 5

                          If you go down this route, you will need to understand how SQL Server applies its rules for implicit datatype conversion.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups