September 11, 2024 at 10:14 am
divided by zero error in SQL please provide solution
September 11, 2024 at 10:18 am
If you have
SELECT x / y
and y is zero, that is an error. There is no solution as such, only a choice about what you would like to return in cases where the denominator is zero.
What would you like to see? NULL, 0, x, something else?
Or you could CATCH the error and take some other action.
September 11, 2024 at 1:38 pm
You could use NULLIF
or SET ARITHABORT OFF; SET ANSI_WARNINGS OFF;
SET ARITHABORT OFF;
SET ANSI_WARNINGS OFF;
DECLARE @x as int = 1,
@y as int = 0
select @x/@y
SET ARITHABORT ON;
SET ANSI_WARNINGS ON;
select @x / NULLIF(@y, 0)
September 11, 2024 at 1:39 pm
.
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply