Multiple Else Statement Logic in SQL
Hi I have a question on using multiple else statements without re-using if statements.
If FieldA = '14'
Set FieldX = '1'
Else If FieldB ='15'
Set FieldX = '2'
Else If FieldC = '16'
Set FieldX ='3'
Else
Set FieldX = '10'
My question is, say both FieldB = 15, and FieldC = 16. So the last two conditions are satisfied but the first isn't. Does the line stop as soon as the Else statement is met? Or will it continue to the next Else statement to test for fieldC?
sql if-statement
add a comment |
Hi I have a question on using multiple else statements without re-using if statements.
If FieldA = '14'
Set FieldX = '1'
Else If FieldB ='15'
Set FieldX = '2'
Else If FieldC = '16'
Set FieldX ='3'
Else
Set FieldX = '10'
My question is, say both FieldB = 15, and FieldC = 16. So the last two conditions are satisfied but the first isn't. Does the line stop as soon as the Else statement is met? Or will it continue to the next Else statement to test for fieldC?
sql if-statement
6
1. That's not SQL. 2. You could easily test it yourself. 3. There is a hint in the word else.
– melpomene
Nov 24 '18 at 7:04
Please identify which DBMS you're using. What you show is not a standard SQL, though it could easily be part of the language some DBMS supports. As to your question, the way that such statements work in most languages is that the first term that evaluates to true executes the corresponding action and terminates the overallIFsequence. So ifFieldB = '15',FieldXwill be set to'2'and not to'3'.
– Jonathan Leffler
Nov 24 '18 at 7:09
It's quite simple. If the condition is met, then there's no reason to do the ELSE. If you want to change which value is returned, then just change the order of the evaluations. Btw, in an actual SQL statement a CASE is often used for such logic.
– LukStorms
Nov 24 '18 at 10:56
add a comment |
Hi I have a question on using multiple else statements without re-using if statements.
If FieldA = '14'
Set FieldX = '1'
Else If FieldB ='15'
Set FieldX = '2'
Else If FieldC = '16'
Set FieldX ='3'
Else
Set FieldX = '10'
My question is, say both FieldB = 15, and FieldC = 16. So the last two conditions are satisfied but the first isn't. Does the line stop as soon as the Else statement is met? Or will it continue to the next Else statement to test for fieldC?
sql if-statement
Hi I have a question on using multiple else statements without re-using if statements.
If FieldA = '14'
Set FieldX = '1'
Else If FieldB ='15'
Set FieldX = '2'
Else If FieldC = '16'
Set FieldX ='3'
Else
Set FieldX = '10'
My question is, say both FieldB = 15, and FieldC = 16. So the last two conditions are satisfied but the first isn't. Does the line stop as soon as the Else statement is met? Or will it continue to the next Else statement to test for fieldC?
sql if-statement
sql if-statement
asked Nov 24 '18 at 7:01
Dan HayesDan Hayes
31
31
6
1. That's not SQL. 2. You could easily test it yourself. 3. There is a hint in the word else.
– melpomene
Nov 24 '18 at 7:04
Please identify which DBMS you're using. What you show is not a standard SQL, though it could easily be part of the language some DBMS supports. As to your question, the way that such statements work in most languages is that the first term that evaluates to true executes the corresponding action and terminates the overallIFsequence. So ifFieldB = '15',FieldXwill be set to'2'and not to'3'.
– Jonathan Leffler
Nov 24 '18 at 7:09
It's quite simple. If the condition is met, then there's no reason to do the ELSE. If you want to change which value is returned, then just change the order of the evaluations. Btw, in an actual SQL statement a CASE is often used for such logic.
– LukStorms
Nov 24 '18 at 10:56
add a comment |
6
1. That's not SQL. 2. You could easily test it yourself. 3. There is a hint in the word else.
– melpomene
Nov 24 '18 at 7:04
Please identify which DBMS you're using. What you show is not a standard SQL, though it could easily be part of the language some DBMS supports. As to your question, the way that such statements work in most languages is that the first term that evaluates to true executes the corresponding action and terminates the overallIFsequence. So ifFieldB = '15',FieldXwill be set to'2'and not to'3'.
– Jonathan Leffler
Nov 24 '18 at 7:09
It's quite simple. If the condition is met, then there's no reason to do the ELSE. If you want to change which value is returned, then just change the order of the evaluations. Btw, in an actual SQL statement a CASE is often used for such logic.
– LukStorms
Nov 24 '18 at 10:56
6
6
1. That's not SQL. 2. You could easily test it yourself. 3. There is a hint in the word else.
– melpomene
Nov 24 '18 at 7:04
1. That's not SQL. 2. You could easily test it yourself. 3. There is a hint in the word else.
– melpomene
Nov 24 '18 at 7:04
Please identify which DBMS you're using. What you show is not a standard SQL, though it could easily be part of the language some DBMS supports. As to your question, the way that such statements work in most languages is that the first term that evaluates to true executes the corresponding action and terminates the overall
IF sequence. So if FieldB = '15', FieldX will be set to '2' and not to '3'.– Jonathan Leffler
Nov 24 '18 at 7:09
Please identify which DBMS you're using. What you show is not a standard SQL, though it could easily be part of the language some DBMS supports. As to your question, the way that such statements work in most languages is that the first term that evaluates to true executes the corresponding action and terminates the overall
IF sequence. So if FieldB = '15', FieldX will be set to '2' and not to '3'.– Jonathan Leffler
Nov 24 '18 at 7:09
It's quite simple. If the condition is met, then there's no reason to do the ELSE. If you want to change which value is returned, then just change the order of the evaluations. Btw, in an actual SQL statement a CASE is often used for such logic.
– LukStorms
Nov 24 '18 at 10:56
It's quite simple. If the condition is met, then there's no reason to do the ELSE. If you want to change which value is returned, then just change the order of the evaluations. Btw, in an actual SQL statement a CASE is often used for such logic.
– LukStorms
Nov 24 '18 at 10:56
add a comment |
1 Answer
1
active
oldest
votes
The answer is Yes the FieldX will be '2' you can read it here how if and else if workif and elseif
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%2f53455948%2fmultiple-else-statement-logic-in-sql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The answer is Yes the FieldX will be '2' you can read it here how if and else if workif and elseif
add a comment |
The answer is Yes the FieldX will be '2' you can read it here how if and else if workif and elseif
add a comment |
The answer is Yes the FieldX will be '2' you can read it here how if and else if workif and elseif
The answer is Yes the FieldX will be '2' you can read it here how if and else if workif and elseif
answered Nov 24 '18 at 7:32
saurav omarsaurav omar
767
767
add a comment |
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%2f53455948%2fmultiple-else-statement-logic-in-sql%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
6
1. That's not SQL. 2. You could easily test it yourself. 3. There is a hint in the word else.
– melpomene
Nov 24 '18 at 7:04
Please identify which DBMS you're using. What you show is not a standard SQL, though it could easily be part of the language some DBMS supports. As to your question, the way that such statements work in most languages is that the first term that evaluates to true executes the corresponding action and terminates the overall
IFsequence. So ifFieldB = '15',FieldXwill be set to'2'and not to'3'.– Jonathan Leffler
Nov 24 '18 at 7:09
It's quite simple. If the condition is met, then there's no reason to do the ELSE. If you want to change which value is returned, then just change the order of the evaluations. Btw, in an actual SQL statement a CASE is often used for such logic.
– LukStorms
Nov 24 '18 at 10:56