Limit decimal places - SQL Server

Asked

Viewed 1,642 times

-1

Opaa!

I made a select of a table that returns the value of a book with a percentage of adjustment, both negative and positive. But it is returning me a value with several decimal places, and I would like to limit to only 2 decimal places. Someone could help me?

Select command used, in this example I used 10%:

select
liv_titulo, liv_preco as preco_atual,
liv_preco + (liv_preco / 100 * 10) as preco_adicao,
liv_preco - (liv_preco / 100 * 10) as preco_subtracao
from livro 

The return of command was thus:

liv_titulo              preco_atual  preco_adicao  preco_subtracao
Engenharia de Software  55.00        60.500000     49.500000
Engenharia de software  78.01        85.811000     70.209000

I’d like to keep it that way:

liv_titulo              preco_atual  preco_adicao  preco_subtracao
Engenharia de Software  55.00        60.50         49.50
Engenharia de software  78.01        85.81         70.20
  • Here are several examples, depending on the version. https://stackoverflow.com/questions/10380197/sql-rounding-off-to-2-decimal-places

1 answer

0


The simple way is this:

select
liv_titulo, liv_preco as preco_atual,
CONVERT(DEC(10,2), liv_preco + (liv_preco / 100 * 10)) as preco_adicao,
CONVERT(DEC(10,2), liv_preco - (liv_preco / 100 * 10)) as preco_subtracao
from livro

You can use CAST as well.

CAST(CAMPO AS NUMERIC(10,2))

The 10 means the number of decimals, if the number has more than 10 houses it will cause overflow error. 2 means the number of boxes after the comma.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.