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.
qgis field-calculator expression
add a comment |
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.
qgis field-calculator expression
add a comment |
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.
qgis field-calculator expression
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
qgis field-calculator expression
edited Nov 26 at 10:46
asked Nov 26 at 10:35
Joules
2001216
2001216
add a comment |
add a comment |
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
Why not just to useleft("Postcode", 3) || ' ' || right("Postcode", 3)
?
– Taras
Nov 26 at 11:17
Your proposal turnsDD115KP
intoDD1 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
add a comment |
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
Why not just to useleft("Postcode", 3) || ' ' || right("Postcode", 3)
?
– Taras
Nov 26 at 11:17
Your proposal turnsDD115KP
intoDD1 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
add a comment |
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
Why not just to useleft("Postcode", 3) || ' ' || right("Postcode", 3)
?
– Taras
Nov 26 at 11:17
Your proposal turnsDD115KP
intoDD1 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
add a comment |
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
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
edited Nov 26 at 10:59
answered Nov 26 at 10:53
Stefan_Fairphone
946118
946118
Why not just to useleft("Postcode", 3) || ' ' || right("Postcode", 3)
?
– Taras
Nov 26 at 11:17
Your proposal turnsDD115KP
intoDD1 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
add a comment |
Why not just to useleft("Postcode", 3) || ' ' || right("Postcode", 3)
?
– Taras
Nov 26 at 11:17
Your proposal turnsDD115KP
intoDD1 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
add a comment |
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.
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%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
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