Add a character every nth character in QGIS Field Calculator











up vote
1
down vote

favorite












I can't find an expression to add a character every nth character from the right of a string, in field calculator.



As an example, I have a list of UK postcodes which I need to add a space to 3 characters from the right:



From: N165KP To: N16 5KP



Note this has to be done from the right as the first lot of characters (before the space) can vary in length.










share|improve this question




























    up vote
    1
    down vote

    favorite












    I can't find an expression to add a character every nth character from the right of a string, in field calculator.



    As an example, I have a list of UK postcodes which I need to add a space to 3 characters from the right:



    From: N165KP To: N16 5KP



    Note this has to be done from the right as the first lot of characters (before the space) can vary in length.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I can't find an expression to add a character every nth character from the right of a string, in field calculator.



      As an example, I have a list of UK postcodes which I need to add a space to 3 characters from the right:



      From: N165KP To: N16 5KP



      Note this has to be done from the right as the first lot of characters (before the space) can vary in length.










      share|improve this question















      I can't find an expression to add a character every nth character from the right of a string, in field calculator.



      As an example, I have a list of UK postcodes which I need to add a space to 3 characters from the right:



      From: N165KP To: N16 5KP



      Note this has to be done from the right as the first lot of characters (before the space) can vary in length.







      qgis field-calculator expression






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 at 10:46

























      asked Nov 26 at 10:35









      Joules

      2001216




      2001216






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Try this:



          left("Postcode", length("Postcode")-3) + ' ' + right("Postcode", 3)


          It takes all characters of Postcode except the last three ones, adds a space and then appends the last three characters.



          Edit.: I tested this in QGIS 2.18.22






          share|improve this answer























          • Why not just to use left("Postcode", 3) || ' ' || right("Postcode", 3)?
            – Taras
            Nov 26 at 11:17










          • Your proposal turns DD115KP into DD1 5KP (Note the missing 1 in the middle).
            – Stefan_Fairphone
            Nov 26 at 11:23










          • I thought the "Postcode" field is limited to six characters, but it is about n-characters. And I double checked the Note in the question, now it is clear.
            – Taras
            Nov 26 at 11:27











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "79"
          };
          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',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fgis.stackexchange.com%2fquestions%2f303909%2fadd-a-character-every-nth-character-in-qgis-field-calculator%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
          3
          down vote



          accepted










          Try this:



          left("Postcode", length("Postcode")-3) + ' ' + right("Postcode", 3)


          It takes all characters of Postcode except the last three ones, adds a space and then appends the last three characters.



          Edit.: I tested this in QGIS 2.18.22






          share|improve this answer























          • Why not just to use left("Postcode", 3) || ' ' || right("Postcode", 3)?
            – Taras
            Nov 26 at 11:17










          • Your proposal turns DD115KP into DD1 5KP (Note the missing 1 in the middle).
            – Stefan_Fairphone
            Nov 26 at 11:23










          • I thought the "Postcode" field is limited to six characters, but it is about n-characters. And I double checked the Note in the question, now it is clear.
            – Taras
            Nov 26 at 11:27















          up vote
          3
          down vote



          accepted










          Try this:



          left("Postcode", length("Postcode")-3) + ' ' + right("Postcode", 3)


          It takes all characters of Postcode except the last three ones, adds a space and then appends the last three characters.



          Edit.: I tested this in QGIS 2.18.22






          share|improve this answer























          • Why not just to use left("Postcode", 3) || ' ' || right("Postcode", 3)?
            – Taras
            Nov 26 at 11:17










          • Your proposal turns DD115KP into DD1 5KP (Note the missing 1 in the middle).
            – Stefan_Fairphone
            Nov 26 at 11:23










          • I thought the "Postcode" field is limited to six characters, but it is about n-characters. And I double checked the Note in the question, now it is clear.
            – Taras
            Nov 26 at 11:27













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Try this:



          left("Postcode", length("Postcode")-3) + ' ' + right("Postcode", 3)


          It takes all characters of Postcode except the last three ones, adds a space and then appends the last three characters.



          Edit.: I tested this in QGIS 2.18.22






          share|improve this answer














          Try this:



          left("Postcode", length("Postcode")-3) + ' ' + right("Postcode", 3)


          It takes all characters of Postcode except the last three ones, adds a space and then appends the last three characters.



          Edit.: I tested this in QGIS 2.18.22







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 at 10:59

























          answered Nov 26 at 10:53









          Stefan_Fairphone

          946118




          946118












          • Why not just to use left("Postcode", 3) || ' ' || right("Postcode", 3)?
            – Taras
            Nov 26 at 11:17










          • Your proposal turns DD115KP into DD1 5KP (Note the missing 1 in the middle).
            – Stefan_Fairphone
            Nov 26 at 11:23










          • I thought the "Postcode" field is limited to six characters, but it is about n-characters. And I double checked the Note in the question, now it is clear.
            – Taras
            Nov 26 at 11:27


















          • Why not just to use left("Postcode", 3) || ' ' || right("Postcode", 3)?
            – Taras
            Nov 26 at 11:17










          • Your proposal turns DD115KP into DD1 5KP (Note the missing 1 in the middle).
            – Stefan_Fairphone
            Nov 26 at 11:23










          • I thought the "Postcode" field is limited to six characters, but it is about n-characters. And I double checked the Note in the question, now it is clear.
            – Taras
            Nov 26 at 11:27
















          Why not just to use left("Postcode", 3) || ' ' || right("Postcode", 3)?
          – Taras
          Nov 26 at 11:17




          Why not just to use left("Postcode", 3) || ' ' || right("Postcode", 3)?
          – Taras
          Nov 26 at 11:17












          Your proposal turns DD115KP into DD1 5KP (Note the missing 1 in the middle).
          – Stefan_Fairphone
          Nov 26 at 11:23




          Your proposal turns DD115KP into DD1 5KP (Note the missing 1 in the middle).
          – Stefan_Fairphone
          Nov 26 at 11:23












          I thought the "Postcode" field is limited to six characters, but it is about n-characters. And I double checked the Note in the question, now it is clear.
          – Taras
          Nov 26 at 11:27




          I thought the "Postcode" field is limited to six characters, but it is about n-characters. And I double checked the Note in the question, now it is clear.
          – Taras
          Nov 26 at 11:27


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


          • 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%2fgis.stackexchange.com%2fquestions%2f303909%2fadd-a-character-every-nth-character-in-qgis-field-calculator%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

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?