some idea how to select a round numbers in sql server

-1

Someone has some idea of how to select from a table round numbers. I would be grateful for an example or some idea so I can implement it in my BD THANK YOU ...

    
asked by Dieguito Weed 07.11.2018 в 20:26
source

2 answers

0

And if you use a ROUND in the field you want to round up and thus detail the type of rounding you need.

ROUND (numeric_expression, length [ function]) Select Round (@Numero, 2, 0)

This example is of a rounding to 2 decimals.

    
answered by 07.11.2018 / 21:06
source
1

There are many ways to select round numbers from a table, it all depends on what you look for or understand by round number.

For example, with the following functions:

  • round() : Perform a rounding according to the number of decimals (or multiples of 10) that is passed as a parameter
  • cast() : I use it to convert a floating-point number to an integer (which is always round )
  • floor() : It has the same effect as cast() , perhaps it is clearer that it always ignores the decimal part, that is truncates the number, not necessarily what rounds (but if it returns a round number).
  • ceiling() : It also returns an integer, but it is always the ceiling of the number passed, that is, 1.0 returns 1 , but 1.001 returns 2 .

You will find more information in the linked documentation. Here are some examples:

with 
A as (
          select 76.13456 Numero
union all select 151.50 
union all select 49.0199999
)
select   Numero
       , ROUND(Numero, 2) NumeroCon2Decimales
       , ROUND(Numero, 0) NumeroComaFlotanteCon0Decimales
       , ROUND(Numero, -1) NumeroAMultiplosde10
       , CAST(Numero as int) NumeroEnteroTruncado1
       , FLOOR(Numero) NumeroEnteroTruncado2
       , CEILING(Numero) NumeroEnteroSuperior
  from A;

What produces this output:

Numero       NumeroCon2Decimales NumeroComaFlotanteCon0Decimales NumeroAMultiplosde10 NumeroEnteroTruncado1 NumeroEnteroTruncado2 NumeroEnteroSuperior
------------ ------------------- ------------------------------- -------------------- --------------------- --------------------- --------------------
76.1345600   76.1300000          76.0000000                      80.0000000           76                    76                    77
151.5000000  151.5000000         152.0000000                     150.0000000          151                   151                   152
49.0199999   49.0200000          49.0000000                      50.0000000           49                    49                    50

(3 row(s) affected)
    
answered by 07.11.2018 в 21:01