By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE Sales(
[ID] INT NOT NULL,
[Income] INT NULL,
[Visits] INT NULL
);
INSERT INTO Sales([ID],[Income],[Visits])
VALUES
(1,1200,30),
(2,0,22),
(3,800,1),
(4,749,7),
(5,192,0),
(6,974,10);
6 rows affected
SELECT *
FROM Sales
ID | Income | Visits |
---|---|---|
1 | 1200 | 30 |
2 | 0 | 22 |
3 | 800 | 1 |
4 | 749 | 7 |
5 | 192 | 0 |
6 | 974 | 10 |
SELECT [ID],[Income]/[Visits]
FROM Sales
Msg 8134 Level 16 State 1 Line 1
Divide by zero error encountered.
SELECT [ID],[Income]/NULLIF([Visits],0)
FROM Sales
ID | (No column name) |
---|---|
1 | 40 |
2 | 0 |
3 | 800 |
4 | 107 |
5 | null |
6 | 97 |