Using NULLIF to divide by zero
This is the error I'm receiving below:
Divide by zero error encountered.
Warning: Null value is eliminated by an aggregate or other SET operation.
Having looked around at solutions it looks like I need to use NULLIF or ISNULL, I'm just unsure as to how to include it within the line below.
select
min(round(OnCallTime*100/TotalTime,1)) as total
I'm using SQL Management Studio 2012
sql sql-server tsql
add a comment |
This is the error I'm receiving below:
Divide by zero error encountered.
Warning: Null value is eliminated by an aggregate or other SET operation.
Having looked around at solutions it looks like I need to use NULLIF or ISNULL, I'm just unsure as to how to include it within the line below.
select
min(round(OnCallTime*100/TotalTime,1)) as total
I'm using SQL Management Studio 2012
sql sql-server tsql
add a comment |
This is the error I'm receiving below:
Divide by zero error encountered.
Warning: Null value is eliminated by an aggregate or other SET operation.
Having looked around at solutions it looks like I need to use NULLIF or ISNULL, I'm just unsure as to how to include it within the line below.
select
min(round(OnCallTime*100/TotalTime,1)) as total
I'm using SQL Management Studio 2012
sql sql-server tsql
This is the error I'm receiving below:
Divide by zero error encountered.
Warning: Null value is eliminated by an aggregate or other SET operation.
Having looked around at solutions it looks like I need to use NULLIF or ISNULL, I'm just unsure as to how to include it within the line below.
select
min(round(OnCallTime*100/TotalTime,1)) as total
I'm using SQL Management Studio 2012
sql sql-server tsql
sql sql-server tsql
edited Nov 23 '18 at 9:37
Mayank Porwal
4,8952724
4,8952724
asked Nov 23 '18 at 9:16
Clem_FandangoClem_Fandango
509
509
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Use NULLIF
in denominator like below:
select
min(round((OnCallTime*100/NULLIF(TotalTime,0)),1)) as total
So, whenever TotalTime
is zero, it'll be replaced by NULL
and you will not get the error of Division by Zero
.
Excellent, thanks Mayank
– Clem_Fandango
Nov 23 '18 at 9:25
@Clem_Fandango Please accept and upvote the answer since it worked.
– Mayank Porwal
Nov 23 '18 at 9:26
add a comment |
use coalesce()
function
select
min(round(OnCallTime*100/coalesce(TotalTime,1),1)) as total
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn
– fa06
Nov 23 '18 at 9:22
if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?
– saravanatn
Nov 23 '18 at 9:39
add a comment |
So in the event TotalTime=0 what do you want to display?. I suggest NULL since divide by zero is not defined?.
You can write the query as follows
select
min(round(
(OnCallTime*100)
/ (case when TotalTime =0 then null else TotalTime end)
,1
)
) as total
add a comment |
use coalesce()
which return 1st non null value
select min(round((OnCallTime)*100/coalesce(TotalTime,1),1)) as total
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443677%2fusing-nullif-to-divide-by-zero%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use NULLIF
in denominator like below:
select
min(round((OnCallTime*100/NULLIF(TotalTime,0)),1)) as total
So, whenever TotalTime
is zero, it'll be replaced by NULL
and you will not get the error of Division by Zero
.
Excellent, thanks Mayank
– Clem_Fandango
Nov 23 '18 at 9:25
@Clem_Fandango Please accept and upvote the answer since it worked.
– Mayank Porwal
Nov 23 '18 at 9:26
add a comment |
Use NULLIF
in denominator like below:
select
min(round((OnCallTime*100/NULLIF(TotalTime,0)),1)) as total
So, whenever TotalTime
is zero, it'll be replaced by NULL
and you will not get the error of Division by Zero
.
Excellent, thanks Mayank
– Clem_Fandango
Nov 23 '18 at 9:25
@Clem_Fandango Please accept and upvote the answer since it worked.
– Mayank Porwal
Nov 23 '18 at 9:26
add a comment |
Use NULLIF
in denominator like below:
select
min(round((OnCallTime*100/NULLIF(TotalTime,0)),1)) as total
So, whenever TotalTime
is zero, it'll be replaced by NULL
and you will not get the error of Division by Zero
.
Use NULLIF
in denominator like below:
select
min(round((OnCallTime*100/NULLIF(TotalTime,0)),1)) as total
So, whenever TotalTime
is zero, it'll be replaced by NULL
and you will not get the error of Division by Zero
.
answered Nov 23 '18 at 9:18
Mayank PorwalMayank Porwal
4,8952724
4,8952724
Excellent, thanks Mayank
– Clem_Fandango
Nov 23 '18 at 9:25
@Clem_Fandango Please accept and upvote the answer since it worked.
– Mayank Porwal
Nov 23 '18 at 9:26
add a comment |
Excellent, thanks Mayank
– Clem_Fandango
Nov 23 '18 at 9:25
@Clem_Fandango Please accept and upvote the answer since it worked.
– Mayank Porwal
Nov 23 '18 at 9:26
Excellent, thanks Mayank
– Clem_Fandango
Nov 23 '18 at 9:25
Excellent, thanks Mayank
– Clem_Fandango
Nov 23 '18 at 9:25
@Clem_Fandango Please accept and upvote the answer since it worked.
– Mayank Porwal
Nov 23 '18 at 9:26
@Clem_Fandango Please accept and upvote the answer since it worked.
– Mayank Porwal
Nov 23 '18 at 9:26
add a comment |
use coalesce()
function
select
min(round(OnCallTime*100/coalesce(TotalTime,1),1)) as total
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn
– fa06
Nov 23 '18 at 9:22
if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?
– saravanatn
Nov 23 '18 at 9:39
add a comment |
use coalesce()
function
select
min(round(OnCallTime*100/coalesce(TotalTime,1),1)) as total
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn
– fa06
Nov 23 '18 at 9:22
if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?
– saravanatn
Nov 23 '18 at 9:39
add a comment |
use coalesce()
function
select
min(round(OnCallTime*100/coalesce(TotalTime,1),1)) as total
use coalesce()
function
select
min(round(OnCallTime*100/coalesce(TotalTime,1),1)) as total
answered Nov 23 '18 at 9:17
fa06fa06
12.7k2917
12.7k2917
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn
– fa06
Nov 23 '18 at 9:22
if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?
– saravanatn
Nov 23 '18 at 9:39
add a comment |
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn
– fa06
Nov 23 '18 at 9:22
if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?
– saravanatn
Nov 23 '18 at 9:39
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn
– fa06
Nov 23 '18 at 9:22
what coalesce(TotalTime,1) will rerurn ? does it return 0 ?? @saravanatn
– fa06
Nov 23 '18 at 9:22
if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?
– saravanatn
Nov 23 '18 at 9:39
if TotalTime=null means it will take it as 1. But TotalTime=0 means anything divide by zero is infinity right?
– saravanatn
Nov 23 '18 at 9:39
add a comment |
So in the event TotalTime=0 what do you want to display?. I suggest NULL since divide by zero is not defined?.
You can write the query as follows
select
min(round(
(OnCallTime*100)
/ (case when TotalTime =0 then null else TotalTime end)
,1
)
) as total
add a comment |
So in the event TotalTime=0 what do you want to display?. I suggest NULL since divide by zero is not defined?.
You can write the query as follows
select
min(round(
(OnCallTime*100)
/ (case when TotalTime =0 then null else TotalTime end)
,1
)
) as total
add a comment |
So in the event TotalTime=0 what do you want to display?. I suggest NULL since divide by zero is not defined?.
You can write the query as follows
select
min(round(
(OnCallTime*100)
/ (case when TotalTime =0 then null else TotalTime end)
,1
)
) as total
So in the event TotalTime=0 what do you want to display?. I suggest NULL since divide by zero is not defined?.
You can write the query as follows
select
min(round(
(OnCallTime*100)
/ (case when TotalTime =0 then null else TotalTime end)
,1
)
) as total
answered Nov 23 '18 at 9:24
George JosephGeorge Joseph
1,47559
1,47559
add a comment |
add a comment |
use coalesce()
which return 1st non null value
select min(round((OnCallTime)*100/coalesce(TotalTime,1),1)) as total
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
add a comment |
use coalesce()
which return 1st non null value
select min(round((OnCallTime)*100/coalesce(TotalTime,1),1)) as total
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
add a comment |
use coalesce()
which return 1st non null value
select min(round((OnCallTime)*100/coalesce(TotalTime,1),1)) as total
use coalesce()
which return 1st non null value
select min(round((OnCallTime)*100/coalesce(TotalTime,1),1)) as total
edited Nov 23 '18 at 9:24
answered Nov 23 '18 at 9:18
Zaynul Abadin TuhinZaynul Abadin Tuhin
12.7k2932
12.7k2932
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
add a comment |
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
Divide by zero error means denominator is zero. In this case it won't work.
– saravanatn
Nov 23 '18 at 9:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443677%2fusing-nullif-to-divide-by-zero%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown