Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to write an ELSEIF conditional statement in QGIS Field Calculator (version 1.8.0). I have used an example I found online:

CASE WHEN val < 0 THEN 'negative'
  WHEN val = 0 THEN "neutral'
  ELSE 'positive'
END

I modified the statement as follows:

CASE WHEN  "GRID_ID"  = 1 THEN 'complete'
  ELSEIF  "GRID_ID"  = 2 THEN "in progress'
  ELSE 'not started'
END

This statement would not run, the Output preview stated Expression is invalid. The more info stated: Parser Error: syntax error, unexpected COLUMN_REF, expecting WHEN or ELSE or END

I am wondernig if anyone has had this error, and if they can share what they did to fix it.

Thanks...

share|improve this question
    
GIS-Jonathan, this is a dumb question, but I have had this issue for a few posts, but how do you write the posts to that code reads poperly? In my original post, prior to submitting it, my code looked correct, but upon submitting it, it changed to one line. I would like to know how to make it look proper, so it does not have to be edited by other users. And thank you for the edit. –  Ryan Garnett Nov 2 '12 at 14:24
    
Markdown formatting requires a blank line before the code block, and the code itself needs four spaces indentation at the beginning of each line; then, it should appear correctly. –  Simbamangu Nov 2 '12 at 17:53
    
Thank you, I will try that next time... –  Ryan Garnett Nov 2 '12 at 18:21

1 Answer 1

up vote 7 down vote accepted

You have a few problems in your modified statement.

  • Inconsistent use of quotes around "in progress'
  • You don't need quotes around column names.
  • You're using an "ELSEIF" when it should be a "WHEN".

The following should resolve all three issues and works for me in 1.8.0:

CASE WHEN GRID_ID = 1 THEN 'complete'
  WHEN GRID_ID = 2 THEN 'in progress'
  ELSE 'not started'
END
share|improve this answer
1  
Thank you very much, I knew it had to be something really easy. Thanks again. –  Ryan Garnett Nov 2 '12 at 14:17
1  
"You don't need quotes around column names." You don't but I would still recommend it as it will help the syntax highlighter mark that part as a column. –  Nathan W Nov 3 '12 at 1:20
    
@NathanW - the Syntax highlighter marks column names in red whether you use quotes or not, at least it does in my 1.8.0 install. –  GIS-Jonathan Nov 5 '12 at 12:23
1  
It does indeed. That is pretty embarrassing, I should have known it does that as I wrote the highlighter ;) –  Nathan W Nov 5 '12 at 13:17
    
@NathanW - Understandable. I can barely remember what I wrote yesterday after all. ;-) –  GIS-Jonathan Nov 5 '12 at 14:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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