Assign “all values” instead of “null” in Conditional Where clause

oracle

I have to extract from a table based on user input. I have a table named Vehicle_Register with vehicle details and users are allowed to filter the data using vehicle type (car, bus, truck…). If no filer is selected all the data should be displayed.

Now for this I have to use two queries, 1 with filter and 2 for all:

  1. select * from vehicle_register where vehicle_type = 'car';
  2. select * from vehicle_register;

Can these two queries be merged? Just want to know how to use a conditional where clause using "case" or "if" statement but to assign vehicle_type = all types when no filter is selected.

Best Answer

You can do something like this :

SELECT *
FROM vehicle_register
WHERE (vehicle_type = @param OR @param is null)

You are using parameterized queries and not just a query build by string concatination in the frontend I hope.