1
1
180Ringing2018-08-08 13:23:38
SQL Server
180Ringing, 2018-08-08 13:23:38

SQL SERVER arithmetic overflow error. Why such behaviour?

Maybe a pretty simple question.
But I would like to know how it works.
Why

SELECT 2000000000 * 3 
--Ошибка арифметического переполнения при преобразовании expression к типу данных int.

SELECT 3000000000 * 3  
-- 9000000000

If you create a view
create view ts
as 

SELECT 2000000000 * 3 AS A, 3000000000 * 3  AS B

, then in data types we will see this
5b6ac47caf1c0980349547.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan., 2018-08-08
@LaRN

This happens because 2000000000 still fits in an int, but 3000000000 doesn't.
https://docs.microsoft.com/en-us/sql/t-sql/data-ty...
int -2^31 (-2 147 483 648) to 2^31-1 (2 147 483 647 )
If you need nothing to collapse, you can do this:
SELECT 2000000000.0 * 3 AS A, 3000000000.0 * 3 AS B
With this option, there will already be a float

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question