Building query to filter for date using QGIS

datefilterqgis

I am using QGIS.

I want to filter for all datapoints that were taken over a specific period: 01/01/2015 to 31/12/2015. If I use the 'filter features using form' I can manage this, however I wanted to do it in the Query Builder.

This is the Specific Filter Expression I use:

"Date">'01/01/2015' AND "Date"<'31/12/2015'

(As "Date" is the field column in my data sheet)

When I submit this however, in addition to the second filter I am applying ("Label", and I have no issue with this filter), I find that the date filter has been completely disregarded, and my data is only filtered by "Label".

How do I filter for a specific date duration?

Best Answer

As @Vince already mentioned, this is not a date but a string. You need to convert it to a date first, then you can do the comparison. You can use to_date() function in field calculator like:

to_date('01/01/2015','dd/MM/yyyy')

so your expression would look something like:

"Date">to_date('01/01/2015','dd/MM/yyyy') AND "Date"<to_date('31/12/2015','dd/MM/yyyy')

Note that this of course also will only work if your Date field is actually a date and not just another string. If it is a string as well, you need to convert it to a date first in the same way. E.g.

to_date("Date",'dd/MM/yyyy')>to_date('01/01/2015','dd/MM/yyyy') AND to_date("Date",'dd/MM/yyyy')<to_date('31/12/2015','dd/MM/yyyy')
Related Question