In the attribute table I have a column "the end of the contract" with date. I want QGIS to show me the label "!" when there will be 30 days or less until the end of the contract.


I Would like to expand on Ian's answer and the comment you made:

You have to convert the now() also into a date because now() also holds the current time. This will yield an interval. So you convert it into day and get an integer value which can be compared.

The final query looks like this:

if( day( to_date( '2019-02-26') - to_date(now()))  <30, '!', 'Some other Label')

The date in here can be any field which holds a valid date

Edit: For labeling you should try:

day( to_date( '2019-02-26') - to_date(now()))  <30

and as label insert your '!' as in this screenshot

enter image description here

  • doesn't work :( – Monika Marczuk Jul 27 '18 at 6:49
  • @MonikaMarczuk whats the error message you receive? – LaughU Jul 27 '18 at 6:53
  • there's no error, it doesn't show anything on the map, the code works in the attribute table, it colors the field in red when the date is close – Monika Marczuk Jul 27 '18 at 6:57
  • glad I could help :) – LaughU Jul 27 '18 at 7:25
  • @MonikaMarczuk sorry I dont understand your question. are you talking about the labels? maybe post it as a new question and explain your issue in detail – LaughU Jul 27 '18 at 8:16

You can use an expression to control when a label is shown, so in this case you could use something like

DateCol -  Now() < 30

To turn on a ! label.

  • thanks, but doesn't work, show me "The conversion "2019-02-26" to double failed" – Monika Marczuk Jul 26 '18 at 12:00
  • You probably need to wrap your column in a date parser. – Ian Turton Jul 26 '18 at 12:08
  • doesn't work :( – Monika Marczuk Jul 26 '18 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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