5 pts.
 Ignoring fields in SQL Where Statement
Can fields in a where statment be ingnored. Such as Where name = :name and date = :date, but if I just pass in a name I'd like the where to select only on the name and ignore the date.

Software/Hardware used:
ASKED: October 31, 2008  8:13 PM
UPDATED: November 3, 2008  4:47 PM

Answer Wiki:
In oracle exists a function NVL(a,b) which will return b if a is null. In sql server I think you can achieve the same by using COALESCE or isNull functions. I don't know if you can use some of these functions in AS/400. I think COALESCE is based on the ANSI SQL standard, so it is possible that you can use it. If that's the case, the query should be constructed like this: <pre>select * from your_table where name = :name and date = coalesce(:date,date)</pre> This way, if :date is null, the date field will be compared to itself, which produces the same result as if the condition did not exist. Hope this helps.
Last Wiki Answer Submitted:  October 31, 2008  8:48 pm  by  carlosdl   63,535 pts.
All Answer Wiki Contributors:  carlosdl   63,535 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Hi,

I normally do something along these lines :-

Select xxxxx
From xxxxx
Where (name = :name or :name = ”) and (date = :date or :date = 0)

May not be the nicest way of doing this, but it works.

Regards,

Martin Gilbert.

 23,625 pts.