Impala - How to get the third to last occurrence of a character within a string











up vote
0
down vote

favorite












I've the following string:



SELECT '00000-AAA1-1111-BBBB1-010101-CCCC1' as Word
FROM Table_A


I have the above string I only want to extract the string between the third-to-last and the last occurence.



Basically, I want to extract the following string:



'BBBB1'


I was trying with that but it gives me the wrong result:



 substr(Word, 1, length(a.cod_system_shipment_detail) - length(reverse(split_part(reverse(Word),'|',1))))

"00000-AAA1-1111-BBBB1-010101-"


How can extract only BBBB1 from the above string?



Thanks!










share|improve this question






















  • it is not clear what you are expecting. what do you mean third to last and last occurrence? I would suggest add some additional explanation
    – hlagos
    Nov 14 at 13:36










  • From "'00000-AAA1-1111-BBBB1-010101-CCCC1'" I want to extract BBBB1. Before 'BBBB1' we have 3 special characters and 2 after.
    – Pedro Alves
    Nov 14 at 15:05










  • before bbbb1 I see "1111-" not sure what you mean by specifial characters
    – hlagos
    Nov 14 at 15:08










  • special character: "-"
    – Pedro Alves
    Nov 14 at 16:27















up vote
0
down vote

favorite












I've the following string:



SELECT '00000-AAA1-1111-BBBB1-010101-CCCC1' as Word
FROM Table_A


I have the above string I only want to extract the string between the third-to-last and the last occurence.



Basically, I want to extract the following string:



'BBBB1'


I was trying with that but it gives me the wrong result:



 substr(Word, 1, length(a.cod_system_shipment_detail) - length(reverse(split_part(reverse(Word),'|',1))))

"00000-AAA1-1111-BBBB1-010101-"


How can extract only BBBB1 from the above string?



Thanks!










share|improve this question






















  • it is not clear what you are expecting. what do you mean third to last and last occurrence? I would suggest add some additional explanation
    – hlagos
    Nov 14 at 13:36










  • From "'00000-AAA1-1111-BBBB1-010101-CCCC1'" I want to extract BBBB1. Before 'BBBB1' we have 3 special characters and 2 after.
    – Pedro Alves
    Nov 14 at 15:05










  • before bbbb1 I see "1111-" not sure what you mean by specifial characters
    – hlagos
    Nov 14 at 15:08










  • special character: "-"
    – Pedro Alves
    Nov 14 at 16:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've the following string:



SELECT '00000-AAA1-1111-BBBB1-010101-CCCC1' as Word
FROM Table_A


I have the above string I only want to extract the string between the third-to-last and the last occurence.



Basically, I want to extract the following string:



'BBBB1'


I was trying with that but it gives me the wrong result:



 substr(Word, 1, length(a.cod_system_shipment_detail) - length(reverse(split_part(reverse(Word),'|',1))))

"00000-AAA1-1111-BBBB1-010101-"


How can extract only BBBB1 from the above string?



Thanks!










share|improve this question













I've the following string:



SELECT '00000-AAA1-1111-BBBB1-010101-CCCC1' as Word
FROM Table_A


I have the above string I only want to extract the string between the third-to-last and the last occurence.



Basically, I want to extract the following string:



'BBBB1'


I was trying with that but it gives me the wrong result:



 substr(Word, 1, length(a.cod_system_shipment_detail) - length(reverse(split_part(reverse(Word),'|',1))))

"00000-AAA1-1111-BBBB1-010101-"


How can extract only BBBB1 from the above string?



Thanks!







impala substr






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 10:49









Pedro Alves

150111




150111












  • it is not clear what you are expecting. what do you mean third to last and last occurrence? I would suggest add some additional explanation
    – hlagos
    Nov 14 at 13:36










  • From "'00000-AAA1-1111-BBBB1-010101-CCCC1'" I want to extract BBBB1. Before 'BBBB1' we have 3 special characters and 2 after.
    – Pedro Alves
    Nov 14 at 15:05










  • before bbbb1 I see "1111-" not sure what you mean by specifial characters
    – hlagos
    Nov 14 at 15:08










  • special character: "-"
    – Pedro Alves
    Nov 14 at 16:27


















  • it is not clear what you are expecting. what do you mean third to last and last occurrence? I would suggest add some additional explanation
    – hlagos
    Nov 14 at 13:36










  • From "'00000-AAA1-1111-BBBB1-010101-CCCC1'" I want to extract BBBB1. Before 'BBBB1' we have 3 special characters and 2 after.
    – Pedro Alves
    Nov 14 at 15:05










  • before bbbb1 I see "1111-" not sure what you mean by specifial characters
    – hlagos
    Nov 14 at 15:08










  • special character: "-"
    – Pedro Alves
    Nov 14 at 16:27
















it is not clear what you are expecting. what do you mean third to last and last occurrence? I would suggest add some additional explanation
– hlagos
Nov 14 at 13:36




it is not clear what you are expecting. what do you mean third to last and last occurrence? I would suggest add some additional explanation
– hlagos
Nov 14 at 13:36












From "'00000-AAA1-1111-BBBB1-010101-CCCC1'" I want to extract BBBB1. Before 'BBBB1' we have 3 special characters and 2 after.
– Pedro Alves
Nov 14 at 15:05




From "'00000-AAA1-1111-BBBB1-010101-CCCC1'" I want to extract BBBB1. Before 'BBBB1' we have 3 special characters and 2 after.
– Pedro Alves
Nov 14 at 15:05












before bbbb1 I see "1111-" not sure what you mean by specifial characters
– hlagos
Nov 14 at 15:08




before bbbb1 I see "1111-" not sure what you mean by specifial characters
– hlagos
Nov 14 at 15:08












special character: "-"
– Pedro Alves
Nov 14 at 16:27




special character: "-"
– Pedro Alves
Nov 14 at 16:27












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










If you want to split by - and get the third element, you can do it as follow



SELECT split_part('00000-AAA1-1111-BBBB1-010101-CCCC1', '-',4);





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%2f53260563%2fimpala-how-to-get-the-third-to-last-occurrence-of-a-character-within-a-string%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








    up vote
    1
    down vote



    accepted










    If you want to split by - and get the third element, you can do it as follow



    SELECT split_part('00000-AAA1-1111-BBBB1-010101-CCCC1', '-',4);





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      If you want to split by - and get the third element, you can do it as follow



      SELECT split_part('00000-AAA1-1111-BBBB1-010101-CCCC1', '-',4);





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        If you want to split by - and get the third element, you can do it as follow



        SELECT split_part('00000-AAA1-1111-BBBB1-010101-CCCC1', '-',4);





        share|improve this answer












        If you want to split by - and get the third element, you can do it as follow



        SELECT split_part('00000-AAA1-1111-BBBB1-010101-CCCC1', '-',4);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 at 1:39









        hlagos

        3,5681817




        3,5681817






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53260563%2fimpala-how-to-get-the-third-to-last-occurrence-of-a-character-within-a-string%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

            Bressuire

            Vorschmack

            Quarantine