0

I have a layer with multiple columns as shown in the picture below. If I want to make another column giving 'match' when column 'layer' and 'left(dista' are same value. How can I apply this in field calculator using if(conditional) or something else?

I used following conditionals; if('layer'='left(dista', 'match', 'unmatch') but it's showing errors

left function is called with wrong number of arguments. Expected 2 but got 1.
syntax error, unexpected COMMA, expecting $end
syntax error, unexpected COMMA, expecting $end
syntax error, unexpected ')', expecting $end

enter image description here

3
  • 1
    your image is missing and you should explain your table structure, field names...QGIS expressions are expecting fieldnames in double quotes, strings in single quotes and functions like left without quotes. The error message says, that your left function is missing one argument like: left("fieldname",x)
    – eurojam
    Jan 6, 2020 at 4:50
  • I've edited the photo. left is not a function but a name of the column.
    – jinha kim
    Jan 6, 2020 at 5:21
  • 2
    you should use double quotes for fieldnames: if("layer"="left(distance)", 'match', 'unmatch')
    – eurojam
    Jan 6, 2020 at 5:34

1 Answer 1

1

Usually, you can choose between two ways to format a field name in the Field Calculator:

  • If the field name has no spaces, you can use only the field name, like this: fieldname

  • If the field name has spaces or resembles a field calculator function, you must enclose the name in doublequotes, like this: "field name"

Your field, "left(dista" looks like the Field Calculator function left(). It looks like the Field Calculator is attempting to run the left() function instead of retrieving the field value.

That's actually not what I would expect from your function; since you put the field name in singlequotes, I would expect it to evaluate 'left(dista' as a string. It would find that the string 'layer' does not equal the string 'left(dista', so the result would always be 'unmatch'.

Try enclosing each field name in doublequotes, like this:

if("layer"="left(dista", 'match', 'unmatch')

If that doesn't work, it's probably because the Field Calculator gets confused by the parenthesis in the field name. In that case, re-name your fields so they don't have parentheses. Open the layer properties dialogue > source fields tab > Toggle editing mode on > re-type the field name.

enter image description here

2
  • thank you so much! this was really helpful :)
    – jinha kim
    Jan 13, 2020 at 6:24
  • Glad it helped. Did it work with the field name "left(dista" in double quotes? Or did you have to change the field name to fix the issue?
    – csk
    Jan 14, 2020 at 16:04

Your Answer

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

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