Multiple Else Statement Logic in SQL












-1















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?










share|improve this question


















  • 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 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


















-1















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?










share|improve this question


















  • 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 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
















-1












-1








-1








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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
















  • 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 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










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














1 Answer
1






active

oldest

votes


















0














The answer is Yes the FieldX will be '2' you can read it here how if and else if workif and elseif






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    The answer is Yes the FieldX will be '2' you can read it here how if and else if workif and elseif






    share|improve this answer




























      0














      The answer is Yes the FieldX will be '2' you can read it here how if and else if workif and elseif






      share|improve this answer


























        0












        0








        0







        The answer is Yes the FieldX will be '2' you can read it here how if and else if workif and elseif






        share|improve this answer













        The answer is Yes the FieldX will be '2' you can read it here how if and else if workif and elseif







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 7:32









        saurav omarsaurav omar

        767




        767






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Lallio

            Unable to find Lightning Node

            Futebolista