Thursday, 25 May 2017

Return all values if parameter is NULL using SQL?

I am not sure what is the right way to do this.

I basically want to return all values if value for certain filter is not defined.

Get request is made with filters like this:

url.com?filter1=abc&filter2=123&filter4=33

so as filter3was not defined i want to return all values regardless what their filter3 value is. Easy logic right.

But if I try to implement in SQL, I get stuck.

SELECT * from TABLE_NAME WHERE filter1 = $1 AND filter2 = $2 AND filter3 = $3 AND filter4 = $4 .

How can I modify SQL above to respond to undefined/blank value and return all. Or is this even correct strategy.



via Muhammad Umer

No comments:

Post a Comment