Operating system: Windows 10 Pro Version of LibreOffice: Version: 6.2.5.2 (x64) File format: ODB Database engine: Firebird
I want a parameter query to return results even if either or both parameters are not entered by the user. I.e.:
- Parameter1 valid & Parameter2 valid = results based upon both Parameters
- Parameter1 valid & Parameter2 not given = results based upon Parameter1
- Parameter1 not given & Parameter2 valid = results based upon Parameter2
- Parameter1 not given & Parameter2 not given = all results from the tables irrespective of either Parameter
This is my query:
SELECT “Client”.“Client_ID”, “Client”.“FamilyName”, “Client”.“ForeName”, “Status”.“AsylumStatus”, “Status”.“StatusFromDate” FROM “Status”, “Client” WHERE “Status”.“Client_ID” = “Client”.“Client_ID” AND ( LOWER ( “Status”.“AsylumStatus” ) = ( LOWER ( :AsylumStatus ) ) OR ( :AsylumStatus IS NULL ) ) AND “Status”.“StatusFromDate” BETWEEN COALESCE ( :FromDate, ‘2018-01-01’ ) AND CURRENT_DATE ORDER BY “Client”.“FamilyName” ASC
You will notice I have used COALESCE to default to a static FromDate value if none is entered, and this seems to work. I’ve tried using COALESCE also for :AsylumStatus (a VARCHAR datatype) in the same way with a default of ‘a’, but this does not seem to work. What I can’t get my head around is why other combinations of data and non-entries fail. This is what happens in each combination for the above query:
- :AsylumStatus not given & :FromDate given = expected results based upon :FromDate
- :AsylumStatus not given & :FromDate not given = all records, as expected
- :AsylumStatus given & :FromDate given = ERROR “Incorrect type for setString”
- :AsylumStatus given & :FromDate not given = ERROR “Incorrect type for setString”
It appears that providing a value for :AsylumStatus is what is causing the problem. How can I adjust my query to overcome this problem?