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. MSSQL behavior: I need to round instead of truncation when number scale is exceeded

MSSQL behavior: I need to round instead of truncation when number scale is exceeded

Scheduled Pinned Locked Moved Database
sql-servertutorialdatabasesysadminquestion
5 Posts 4 Posters 1 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.
  • P Offline
    P Offline
    px05
    wrote on last edited by
    #1

    Hi! On SQL server, when I try to save value 0.4569 to a decimal(9,3) column, the value is truncated. It becomes 0.456. It there a way how to change this behavior of sql server? I would like such numbers to be rounded (to 0.457 in my example) instead of truncating them. Is there some global setting for this? Thanks!

    G L W 3 Replies Last reply
    0
    • P px05

      Hi! On SQL server, when I try to save value 0.4569 to a decimal(9,3) column, the value is truncated. It becomes 0.456. It there a way how to change this behavior of sql server? I would like such numbers to be rounded (to 0.457 in my example) instead of truncating them. Is there some global setting for this? Thanks!

      G Offline
      G Offline
      GuyThiebaut
      wrote on last edited by
      #2

      Try the round function[^]

      “That which can be asserted without evidence, can be dismissed without evidence.”

      ― Christopher Hitchens

      P 1 Reply Last reply
      0
      • G GuyThiebaut

        Try the round function[^]

        “That which can be asserted without evidence, can be dismissed without evidence.”

        ― Christopher Hitchens

        P Offline
        P Offline
        px05
        wrote on last edited by
        #3

        Yes, I can use ROUND function, off course. But I would like to change the behavior globally so I will not need to use ROUND function everytime I will need to stop sql server from truncating numbers.

        1 Reply Last reply
        0
        • P px05

          Hi! On SQL server, when I try to save value 0.4569 to a decimal(9,3) column, the value is truncated. It becomes 0.456. It there a way how to change this behavior of sql server? I would like such numbers to be rounded (to 0.457 in my example) instead of truncating them. Is there some global setting for this? Thanks!

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

          Only setting that affects rounding that I know of is "NUMERIC_ROUNDABORT". It says "rounded" and not "truncated" in the documentation[^].

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

          1 Reply Last reply
          0
          • P px05

            Hi! On SQL server, when I try to save value 0.4569 to a decimal(9,3) column, the value is truncated. It becomes 0.456. It there a way how to change this behavior of sql server? I would like such numbers to be rounded (to 0.457 in my example) instead of truncating them. Is there some global setting for this? Thanks!

            W Offline
            W Offline
            Wombaticus
            wrote on last edited by
            #5

            You could always add 0.0005 to your numbers before saving them... ;P

            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