SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Rounding a numeric value to nearest integer


Rounding a numeric value to nearest integer

Author
Message
khpcbgnt
khpcbgnt
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 13
Hi ,

I am trying to round the following numbers to nearest integer.
SELECT ROUND(9.6,0)
SELECT ROUND(99.6,0)
The above two statements failing with the following error.
Msg 8115, Level 16, State 2, Line 30
Arithmetic overflow error converting expression to data type numeric.

The following two statements are working fine.
SELECT ROUND(19.6,0)
SELECT ROUND(199.6,0)

Is there a common syntax that I can use to round all the 4 above values to the nearest integer?
Thanks.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)

Group: General Forum Members
Points: 366074 Visits: 41919

Maybe this will shed some light on the subject:


DECLARE @testnum DECIMAL(18,2);
SET @testnum = 9.6;
SELECT ROUND(9.6,0);
SELECT ROUND(@testnum,0);
SET @testnum = 99.6;
SELECT ROUND(99.6,0);
SELECT ROUND(@testnum,0);

SET @testnum = 19.6;
SELECT ROUND(19.6,0);
SELECT ROUND(@testnum,0)
SET @testnum = 199.6;
SELECT ROUND(199.6,0);
SELECT ROUND(@testnum,0);



basically, the first two are trying to add an additional digit of precision to the numeric value while the second two aren't.



Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (816K reputation)SSC Guru (816K reputation)SSC Guru (816K reputation)SSC Guru (816K reputation)SSC Guru (816K reputation)SSC Guru (816K reputation)SSC Guru (816K reputation)SSC Guru (816K reputation)

Group: General Forum Members
Points: 816764 Visits: 46281
Lynn Pettis - Wednesday, January 3, 2018 2:00 PM

Maybe this will shed some light on the subject:


DECLARE @testnum DECIMAL(18,2);
SET @testnum = 9.6;
SELECT ROUND(9.6,0);
SELECT ROUND(@testnum,0);
SET @testnum = 99.6;
SELECT ROUND(99.6,0);
SELECT ROUND(@testnum,0);

SET @testnum = 19.6;
SELECT ROUND(19.6,0);
SELECT ROUND(@testnum,0)
SET @testnum = 199.6;
SELECT ROUND(199.6,0);
SELECT ROUND(@testnum,0);



basically, the first two are trying to add an additional digit of precision to the numeric value while the second two aren't.



Since the result of round is DECIMAL(38,s) for decimal or numeric and FLOAT for float values according to Books Online, this sounds like a serious flaw for the ROUND function. It's amazing to me that after more than 20 years of using the function, I've apparently not ever used it with literals before.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)

Group: General Forum Members
Points: 366074 Visits: 41919
Jeff Moden - Wednesday, January 3, 2018 6:29 PM
Lynn Pettis - Wednesday, January 3, 2018 2:00 PM

Maybe this will shed some light on the subject:


DECLARE @testnum DECIMAL(18,2);
SET @testnum = 9.6;
SELECT ROUND(9.6,0);
SELECT ROUND(@testnum,0);
SET @testnum = 99.6;
SELECT ROUND(99.6,0);
SELECT ROUND(@testnum,0);

SET @testnum = 19.6;
SELECT ROUND(19.6,0);
SELECT ROUND(@testnum,0)
SET @testnum = 199.6;
SELECT ROUND(199.6,0);
SELECT ROUND(@testnum,0);



basically, the first two are trying to add an additional digit of precision to the numeric value while the second two aren't.



Since the result of round is DECIMAL(38,s) for decimal or numeric and FLOAT for float values according to Books Online, this sounds like a serious flaw for the ROUND function. It's amazing to me that after more than 20 years of using the function, I've apparently not ever used it with literals before.


Actually Jeff, I think it is defaulting to the size of the literal values and that is why it is failing because it can't expand the size of the data type.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (816K reputation)SSC Guru (816K reputation)SSC Guru (816K reputation)SSC Guru (816K reputation)SSC Guru (816K reputation)SSC Guru (816K reputation)SSC Guru (816K reputation)SSC Guru (816K reputation)

Group: General Forum Members
Points: 816764 Visits: 46281
Lynn Pettis - Wednesday, January 3, 2018 6:58 PM
Jeff Moden - Wednesday, January 3, 2018 6:29 PM
Lynn Pettis - Wednesday, January 3, 2018 2:00 PM

Maybe this will shed some light on the subject:


DECLARE @testnum DECIMAL(18,2);
SET @testnum = 9.6;
SELECT ROUND(9.6,0);
SELECT ROUND(@testnum,0);
SET @testnum = 99.6;
SELECT ROUND(99.6,0);
SELECT ROUND(@testnum,0);

SET @testnum = 19.6;
SELECT ROUND(19.6,0);
SELECT ROUND(@testnum,0)
SET @testnum = 199.6;
SELECT ROUND(199.6,0);
SELECT ROUND(@testnum,0);



basically, the first two are trying to add an additional digit of precision to the numeric value while the second two aren't.



Since the result of round is DECIMAL(38,s) for decimal or numeric and FLOAT for float values according to Books Online, this sounds like a serious flaw for the ROUND function. It's amazing to me that after more than 20 years of using the function, I've apparently not ever used it with literals before.


Actually Jeff, I think it is defaulting to the size of the literal values and that is why it is failing because it can't expand the size of the data type.


I did a SELECT/INTO with the literals and they came out as DECIMAL(9,1) in the table (5 bytes, 9 places of precision, 1 place of scale).

The real problem is... it doesn't work as expected and there's nothing in the documentation that says it shouldn't. It should be able to handle these problems, literal or not.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)SSC Guru (366K reputation)

Group: General Forum Members
Points: 366074 Visits: 41919
Jeff Moden - Wednesday, January 3, 2018 7:39 PM
Lynn Pettis - Wednesday, January 3, 2018 6:58 PM
Jeff Moden - Wednesday, January 3, 2018 6:29 PM
Lynn Pettis - Wednesday, January 3, 2018 2:00 PM

Maybe this will shed some light on the subject:


DECLARE @testnum DECIMAL(18,2);
SET @testnum = 9.6;
SELECT ROUND(9.6,0);
SELECT ROUND(@testnum,0);
SET @testnum = 99.6;
SELECT ROUND(99.6,0);
SELECT ROUND(@testnum,0);

SET @testnum = 19.6;
SELECT ROUND(19.6,0);
SELECT ROUND(@testnum,0)
SET @testnum = 199.6;
SELECT ROUND(199.6,0);
SELECT ROUND(@testnum,0);



basically, the first two are trying to add an additional digit of precision to the numeric value while the second two aren't.



Since the result of round is DECIMAL(38,s) for decimal or numeric and FLOAT for float values according to Books Online, this sounds like a serious flaw for the ROUND function. It's amazing to me that after more than 20 years of using the function, I've apparently not ever used it with literals before.


Actually Jeff, I think it is defaulting to the size of the literal values and that is why it is failing because it can't expand the size of the data type.


I did a SELECT/INTO with the literals and they came out as DECIMAL(9,1) in the table (5 bytes, 9 places of precision, 1 place of scale).

The real problem is... it doesn't work as expected and there's nothing in the documentation that says it shouldn't. It should be able to handle these problems, literal or not.


I agree.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search