3

I have 600 records that I need to be linked on a website. As clickable urls. But when I have the URL's, they only link to the 1st set of text before a space. So is there a code in QGIS that I can put in to alleviate this. I have tried: replace( "url"," ") with "%20" But this requires some more work.

2
  • 1
    @Joseph that works, great job, I just put the with outside of the brackets. Thanks very much.
    – Matt Sully
    Commented Sep 13, 2017 at 12:17
  • Most welcome! Will post the comment as an answer for the convenience of others :)
    – Joseph
    Commented Sep 13, 2017 at 12:23

2 Answers 2

6

You're close, you just need to put %20 inside the expression surrounded by single quotes (used to denote string) instead of double quotes (used to denote field names).

So you could use:

replace("url", ' ', '%20')

Note that this replaces all spaces in each string so you may need to be mindful, especially in cases where you might have a space at the end of a string.

5
  • I need to finish a query to replace some text in the QGIS query. I have got this so far: replace("url", 'Y:\Natural Environment Record\', 'exmoor-nationalpark.gov.uk/PAttachments//NER/') It through s a error. I have a section of text that I need to replace.
    – Matt Sully
    Commented Sep 13, 2017 at 13:35
  • @MattSully - What error does it throw? I'm guessing it's due to the slashes you used as it will read this if you don't escape them correctly.
    – Joseph
    Commented Sep 13, 2017 at 13:40
  • Parser Error: syntax error, unexpected COLUMN_REF, expecting COMMA or ')' Eval Error: No root node! Parsing failed?
    – Matt Sully
    Commented Sep 13, 2017 at 13:41
  • 1
    try 'Y:\\Natural Environment Record\\' with doubled backslashes
    – Jimmy
    Commented Sep 13, 2017 at 16:19
  • @Jimmy - That answered his next question :)
    – Joseph
    Commented Sep 14, 2017 at 8:54
2

There's more than just space that is not "URL safe".

If you are looking to URL encode your strings, try (for Python 2):

import urllib
url = urllib.urlencode(url)

This will handle spaces and a whole lot more.

This post has a quick table of the different characters you need to encode.

1
  • This no longer works
    – Mike Wise
    Commented Aug 31, 2020 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.