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.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
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
.
September 16, 2024 at 6:44 am
This was removed by the editor as SPAM
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply